Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change path separator in Windows PowerShell

Tags:

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.

like image 994
ctuffli Avatar asked Apr 23 '09 23:04

ctuffli


People also ask

How do I change the path separator in Windows?

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:/

How do I change the path in Windows PowerShell?

What is this? You can also change directory in PowerShell to a specified path. To change directory, enter Set-Location followed by the Path parameter, then the full path you want to change directory to. If the new directory path has spaces, enclose the path in a double-quote (“”).

What is split path in PowerShell?

The Split-Path cmdlet returns only the specified part of a path, such as the parent folder, a subfolder, or a file name. It can also get items that are referenced by the split path and tell whether the path is relative or absolute. You can use this cmdlet to get or submit only a selected part of a path.


2 Answers

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.

like image 108
Matt Hamilton Avatar answered Sep 23 '22 02:09

Matt Hamilton


Replace "\" with "/".

PS C:\Users\dance2die> $path =  "C:\Documents and Settings\Administrator"
PS C:\Users\dance2die> $path.Replace("\", "/")
C:/Documents and Settings/Administrator
like image 29
dance2die Avatar answered Sep 25 '22 02:09

dance2die