Home » PowerShell » How to Use PowerShell Variable in Path

How to Use PowerShell Variable in Path

Using variables in paths is an important aspect while working with PowerShell scripting. It provides flexibility and maintainability of scripts and allows you to store values and reuse them in multiple places.

In this article, we will discuss how to define, access, and manipulate PowerShell variables in the path.

Define and Use Variable in Path

Use the $ symbol to define a variable in PowerShell and assign the value using the = operator.

# Define a variable
$filePath = "D:\PS\get-adusers.txt"

To use the variable in a path with a command, provide the reference of the variable in a script:

Get-Content -Path $filePath

In the above PowerShell script, the Get-Content cmdlet reads the content of the file specified by the variable $filePath.

Accessing Environment Variables in Paths

Use the $env: to access the environment variables in PowerShell. The following script access the Path environment variable:

$envPath = $env:Path

You can access user-specific variables to get path information about a user profile or username using %USERPROFILE% and %USERNAME% variables respectively.

# Access the username path
$userName = $env:USERNAME

# Access the user profile path
$userProfile = $env:USERPROFILE

In the above PowerShell script, the $env:USERPROFILE variable gets the path variable value and stores it in another variable.

In PowerShell to get a variable desktop path, use the following syntax:

$env:USERPROFILE\Desktop 

To use the environment variable in the path, use the following syntax:

cd $env:USERPROFILE\Desktop

The above PowerShell script uses the environment variable path to change a directory into a path.

Handle PowerShell Variable Path with Spaces

While working with paths containing white spaces, you can use double quotes.

Using the double quotes to handle variable path with space:

$filePath = "C:\Program Files\Shell Geek\readme.txt"

The above PowerShell script uses double quotes to handle the path containing the spaces.

Cool Tip: How to check if a variable exists in PowerShell!

PowerShell Store Directory Path in Variables

You can store directory paths in variables and use them in scripts to perform operations.

Let’s take an example, you want to store the file path in a variable and use it with the Copy-Item cmdlet to copy the file from the source path to the destination folder path.

$sourceFilePath = "D:\PS\PowerShell-Convert-variables-to-string.png"                                            $destinationFolderPath = "D:\PS\image\"                                                                         Copy-Item -Path $sourceFilePath -Destination $destinationFolderPath                                                                                                                                                                        

In the above PowerShell script, the Copy-Item command uses the path stored in the variables path to copy the file from source to destination.

Conclusion

I hope the above article on how to use variable paths in PowerShell commands, and the script are helpful to you.

If the path contains the space, ensure to handle spaces with double quotes or the Join-Path cmdlet.

Store the path in the variable for the flexibility and maintainability of your scripts.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.