ctuffli
ctuffli

Reputation: 3580

Change path separator in Windows PowerShell

Is it possible to get PowerShell to always output / instead of \? For example, I'd like the output of get-location to be C:/Documents and Settings/Administrator.

Update

Thanks for the examples of using replace, but I was hoping for this to happen globally (e.g. tab completion, etc.). Based on Matt's observation that the separator is defined by System.IO.Path.DirectorySeparatorChar which appears in practice and from the documentation to be read-only, I'm guessing this isn't possible.

Upvotes: 20

Views: 18593

Answers (3)

Matt Hamilton
Matt Hamilton

Reputation: 204129

It's a good question. The underlying .NET framework surfaces this as System.IO.Path.DirectorySeparatorChar, and it's a read/write property, so I figured you could do this:

[IO.Path]::DirectorySeparatorChar = '/'

... and that appears to succeed, except if you then type this:

[IO.Path]::DirectorySeparatorChar

... it tells you that it's still '\'. It's like it's not "taking hold". Heck, I'm not even sure that PowerShell honours that particular value even if it was changing.

I thought I'd post this (at the risk of it not actually answering your question) in case it helps someone else find the real answer. I'm sure it would be something to do with that DirectorySeparatorChar field.

Upvotes: 13

zdan
zdan

Reputation: 29450

You could create a filter (or function) that you can pipe your paths to:

PS C:\> filter replace-slash {$_ -replace "\\", "/"}
PS C:\> Get-Location | replace-slash
C:/

Upvotes: 4

dance2die
dance2die

Reputation: 36905

Replace "\" with "/".

PS C:\Users\dance2die> $path =  "C:\Documents and Settings\Administrator"
PS C:\Users\dance2die> $path.Replace("\", "/")
C:/Documents and Settings/Administrator

Upvotes: 9

Related Questions