#PSTip Convert a path to WMI compatible path

Note: This tip requires PowerShell 3.0 or above.

Some of the WMI queries require that we specify the folder path with an escaped path separator. For example, take a look at the following error.


So, we need to escape the path separator for the above query to work.


When we are running such queries programmatically for user provided input, we need a better way to convert the path to WMI compatible path.

Here is the trick I use.

'C:\Windows' -replace "\\","\\"

How do you do it?

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts