Home » PowerShell Set-Location

PowerShell Set-Location

by Online Tutorials Library

PowerShell Set-location | PowerShell Change directory

The Set-Location cmdlet sets the current PowerShell working location to a specified location. The location could be a directory, a registry location, a subdirectory, or any provider path. The sl, cd, chdir are the aliases for the Set-Location cmdlet.

Syntax

Parameters

-Path

The -Path parameter is used to specify a path of the new working location. If no path is provided in the cmdlet, this cmdlet sets the path by default to the current user’s home directory. When wildcard characters are used in the cmdlet, it selects the first path that matches the wildcard pattern.

Wildcard keeps a history of the last twenty locations we have set. If the path is the ‘-‘character in the cmdlet, then the new working location becomes a previous working location in history (if it exists). Similarly, if the path is the’+’ character in the cmdlet, then the new working location becomes the next working location in history (if it exists). This cmdlet is similar to using Push-Location and Pop-Location cmdlets except that history is a list, not a stack, and there is no way to show this history list.

-LiteralPath

The -LiteralPath parameter is used to specify a path to one or more locations. Its value is used exactly as it is typed. If the path includes the escape characters, enclose it in a single quotation mark. Single quotation mark tells the Windows PowerShell that it should not interpret any character as an escape sequence.

-PassThru

The -PassThru parameter is used to return a PathInfo object, which represents the location. By default, this cmdlet produces any output.

-StackName

The -StackName parameter is used to specify that this cmdlet makes the current location stack. Type the $null or empty string, to indicate the unnamed default location stack. The *-Location cmdlets cannot act on the current stack unless we can use the -StackName parameter to specify the different stack.

Examples

Example 1: To set the current location

PowerShell Set-Location

The example in this cmdlet sets the current location to the root of HKLM: drive.

Example 2: To set the current location and show that location

PowerShell Set-Location

The cmdlet in this example sets the current location to the root of Env: drive. It uses a -PassThru parameter to direct PowerShell to return a PathInfo object, which represents the Env: location.

Example 3: To set the current location to another drive

PowerShell Set-Location

The cmdlet, in this example, sets the current location to the D:drive.


You may also like