Skip to content

Working with paths in PowerShell

An administrator often has to work with paths. Luckily, PowerShell has a couple of cmdlets that make it easy to work with paths, test their validity or extract just the information you need. The test-path cmdlet checks if the path refers to a valid item. For example:

test-path c:\config.sys

If you want to check for the existence of a file or directory, use the -PathType parameter. As PathType specify Any, Container or Leaf. For example:

test-path c:\config.sys -PathType container

returns False because config.sys is a file (leaf) and not a container.

Test-Path can also be used to check if a path is valid without checking for the existence of the actual item. Use the -IsValid switch for that. For example:

test-path c:\config.sys -IsValid

will return True even in c:\config.sys does not exist. It returns True because the path is constructed in a valid way.

The split-path cmdlet provides some functionality to extract parts of a path. For example, take the following path:

c:\windows\system32\WindowsPowerShell\V1.0\powershell.exe

Now let’s take a look what split-path can do with it.

  • “split-path c:\windows\system32\WindowsPowerShell\V1.0\powershell.exe” returns c:\windows\system32\WindowsPowerShell\V1.0. Without any additional parameters, split-path just returns the parent. It is the same as using the -parent switch.
  • “split-path -leaf c:\windows\system32\WindowsPowerShell\V1.0\powershell.exe” returns powershell.exe.
  • “split-path -qualifier c:\windows\system32\WindowsPowerShell\V1.0\powershell.exe” returns c:. It returns the drive of the path. This could also be HKCU: or HKLM:.

split-path also has a -resolve parameter. This can be handy if you use wildcards in the path. For example, the following command

split-path -leaf -resolve C:\Windows\system32\WindowsPowerShell\v1.0\*.ps1xml

returns a collection of strings of all the file names ending with .ps1xml. Note the difference with “dir *.ps1xml” which returns a collection of FileInfo objects.

The .NET class System.IO.Path contains even more useful methods to work with paths. To call methods of the Path class from PowerShell, put the full name of the class in square brackets followed by :: and the method name. For example, to call the GetExtension method of the Path class, do this:

[System.IO.Path]::GetExtension(”c:\config.sys”)

The above would return ”.sys”. To find out what other methods you can use, type the following:

[System.IO.Path] | gm -static

The above command uses the get-member cmdlet (gm) with the -static switch to get the static methods of the Path class. The static method GetTempFileName can be useful if you need a temporary file in the temp folder ($env:temp). The method creates a 0-byte file in the temp folder and returns the full path to the file as a string. For example:

$tempfile=[System.IO.Path]::GetTempFileName()
dir > $tempfile
invoke-item $tempfile

Another method, GetRandomFileName(), just returns a random file name as a string. It does not use the temp folder and does not create the file either.

There are some other cmdlets that work with paths like JOIN-PATH, CONVERT-PATH and RESOLVE-PATH. You can use the man alias with -full switch to get help about those. For example:

man join-path -full

If you use the PowerShell Community Extensions, you have some other commands at your disposal:

  • GET-SHORTPATH: gets the 8.3 name for a given path (e.g. get-shortpath “c:\program files”)
  • ADD-SHORTPATH: this is a filter (alias=asp) that adds the short path to the output of dir. Use it like this: “dir | asp | ft name,shortpath,length -auto”.

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*