PowerShell: Fix a path variable’s trailing slash

You can use this syntax to remove any trailing slash ‘\’ to a path and add it back in if it’s a string.

# Remove slash
$OutPath = $Path.TrimEnd('\') 

# Remove if exists and add back in
$OutPath = $Path.TrimEnd('\') + '\'

Comments are closed.

Post Navigation