#PSTip Get the fully qualified path of the system directory

There are many ways to get the path of the system directory (e.g system32). Here’ one that I find very useful that involves the .NET Environment class:

PS> [System.Environment]::SystemDirectory

I use it in my PowerShell profile and adds it to my session’s Env drive so I can conveniently query its value  just as I query all environment variables:

# add a new environment variable
PS> $env:SystemDirectory = [Environment]::SystemDirectory

# query it using the $env drive variable
PS> $env:SystemDirectory

Which also makes it very easy to be embedded and expanded in a string:

PS> "The path of the system directory is: $env:SystemDirectory"
The path of the system directory is: C:\WINDOWS\system32

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts

%d bloggers like this: