Converting the FileSystemObject's GetSpecialFolder Method

Windows PowerShell

Definition: Depending on the parameter passed to it, this method returns a reference to the Windows folder, the System folder, or the Temp folder.


You can use the Get-Item cmdlet with the ENV: PowerShell drive to retrieve the environment variables to the Windows and Temp paths.

Windows Folder:

$a = Get-Item env:\windir

Temp Folder:

$a = Get-Item env:\temp

You can find the System folder by referencing the ComSpec environment variable, then using Split-Path with the -parent parameter to remove the file name:

System Folder:

$a = Get-Item env:\comspec
$a = Split-Path $a.value -parent

Another option for retrieving the System folder, which is probably a little more straightforward, is to use WMI:

(Get-WMIObject Win32_OperatingSystem).SystemDirectory

Here we’re using the Get-WMIObject cmdlet to retrieve a Win32_OperatingSystem object. One of the properties of Win32_OperatingSystem is SystemDirectory, which just happens to contain the full path to the System directory. And that’s exactly what we’re looking for.

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