Split-Path – used another way

Split-Path is a very useful command which allows you to do string manipulation with file paths.  It chops up paths into Parent/Child directory names, file names and can also determine if the path in question is relative or absolute etc.  Check out http://technet.microsoft.com/en-us/library/dd315377.aspx for more details.

However, there are times when output from another command is in a format similar to:

a.b@example.comSome_GUID
OR
child.example.com/Some_OU/Display_Name

Note: In example two, they are forward slashes.  Your goal here could be to extract the mailbox address from the first example or Domain/OU information from the second.  Instead of doing the usual string manipulation on this output, you could make it easier by using Split-Path to split the information by either piping the relevant property into the split-path command or using something similar to:

split-path((get-user -anr $_.Address).Identity)

where “Address” is the mail address(es) passed to the command by some mechanism e.g. Import-CSV or ForEach etc.  This particular example will give you the Domain/OU information for that user.

It’s not the most obvious use of Split-Path and certainly not the most authentic but is definitely easy and effective in most cases.  Have a go and see what happens!

By | 2010-04-18T23:23:53+00:00 April 18th, 2010|Powershell|0 Comments

About the Author:

Leave A Comment