#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?

Share on:
comments powered by Disqus