When working with file names, you occasionally have to extract the drive, path, or file name. The cmdlets Get-ChildItem and Split-Path will do the job. The latter is also able to parse paths in the registry or in AD if you mount them as drives.

Wolfgang Sommergut

Wolfgang Sommergut has over 20 years of experience in IT journalism. He has also worked as a system administrator and as a tech consultant. Today he runs the German publication WindowsPro.de.

In principle, you can determine the required file names with Get-ChildItem to isolate the parts you need from the results. For file names, it is relatively simple because you can use the parameter name:

The result is a string or an array of strings, respectively, whereas Get-ChildItem usually returns a FileInfo object. Those objects contain a few useful properties—among them the entire name including the path (FullName), the path alone (DirectoryName), and the file name (Name) without extension (BaseName):

Split-Path offers more options ^

If you face the task of reading a list of file names to parse them, Get-ChildItem is not going to help. Split-Path closes this gap. It can determine the names and paths of file system, AD, or registry objects and you can use it to parse strings if they follow the syntax of file names.

For splitting file names, the following parameters are available:

  • Qualifier (drive, the part before the colon, respectively)
  • NoQualifier (absolute path without drive)
  • Leaf (file name)
  • Parent (parent directory as a relative or absolute path)

Split-Path also accepts wildcards. However, by default, PowerShell does not resolve them. Instead, they appear literally in the result—for instance, in the leaf. You can turn off this behavior with the help of the parameter Resolve.

Examples ^

Split-Path isn’t just restricted to the file system; it can also be applied to the registry and the AD (read Mount Active Directory as a drive in PowerShell).

For instance, if you are in AD:\DC=contoso,DC=com and you enter the following command, you will receive all OUs that are directly located below the domain as distinguished names:

Win the monthly 4sysops member prize for IT pros


Related Posts

  1. Ahmad yamout 9 months ago

    Hello do you know how to get the full directory of a zip folder that has been downloaded and that include xml file. Thank you.


  2. Mauro 1 month ago

    Thanks for your guide, i'm looking a way to have the "parent" directory of a file . (Hope you get the idea)

    for example I have the next path


    And I need folderN


Leave a reply

Your email address will not be published. Required fields are marked *



Please ask IT administration questions in the forum. Any other messages are welcome.

© 4sysops 2006 - 2018

Log in with your credentials


Forgot your details?

Create Account