Export (0) Print
Expand All
Expand Minimize
This topic has not yet been rated - Rate this topic

Converting the FileSystemObject's GetBaseName Method

Windows PowerShell

Definition: Returns the final element in a path, minus any file extension. For example, passing C:\Scripts\Test.txt to the GetBaseName method would return Test. Passing C:\Scripts would return Scripts.


The Split-Path cmdlet in PowerShell works in a similar way, with one exception: Split-Path will return the file extension with the file. To retrieve only the last element of a path call the Split-Path cmdlet with the -leaf parameter:

PS C:\scripts\temp> Split-Path C:\scripts\test.txt -leaf

Did you notice one thing about the preceding example? Yes, that’s right, the file extension is included. GetBaseName returns the filename without the file extension; Split-Path returns the filename with the file extension.

Here’s how to get the final element in a path when that element is a folder:

PS C:\scripts\temp> Split-Path C:\scripts -leaf

See conversions of other FileSystemObject methods and properties.
Return to the VBScript to Windows PowerShell home page

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
© 2014 Microsoft. All rights reserved.