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.

GetBaseName


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
test.txt


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
Scripts


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

Show: