Set-Location
Sets the current working location to a specified location.
Syntax
Set-Location
[[-Path] <String>]
[-PassThru]
[-UseTransaction]
[<CommonParameters>]
Set-Location
-LiteralPath <String>
[-PassThru]
[-UseTransaction]
[<CommonParameters>]
Set-Location
[-PassThru]
[-StackName <String>]
[-UseTransaction]
[<CommonParameters>]
Description
The Set-Location
cmdlet sets the working location to a specified location. That location could be
a directory, a subdirectory, a registry location, or any provider path.
You can also use the StackName parameter to make a named location stack the current location stack. For more information about location stacks, see the Notes.
Examples
Example 1: Set the current location
PS C:\> Set-Location -Path "HKLM:"
PS HKLM:\>
This command sets the current location to the root of the HKLM: drive.
Example 2: Set the current location and display that location
PS C:\> Set-Location -Path "Env:" -PassThru
Path
----
Env:\
PS Env:\>
This command sets the current location to the root of the Env: drive. It uses the PassThru parameter to direct PowerShell to return a PathInfo object that represents the Env: location.
Example 3: Set location to the C: drive
PS C:\> Set-Location C:
This command sets the current location C: drive in the FileSystem provider.
Example 4: Set the current location to a named stack
PS C:\> Set-Location -StackName "WSManPaths"
This command makes the WSManPaths location stack the current location stack.
The *-Location
cmdlets use the current location stack unless a different location stack is
specified in the command. For information about location stacks, see the Notes.
Parameters
-LiteralPath
Specifies a path of the location. The value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcard characters. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell PowerShell not to interpret any characters as escape sequences.
Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.
Type: | String |
Aliases: | PSPath |
Position: | Named |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-PassThru
Returns a PathInfo object that represents the location. By default, this cmdlet does not generate any output.
Type: | SwitchParameter |
Position: | Named |
Default value: | False |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
-Path
Specify the path of a new working location. If no path is provided, Set-Location
defaults to the
current user's home directory. When wildcards are used, the cmdlet chooses the first path that
matches the wildcard pattern.
Type: | String |
Position: | 0 |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | True |
-StackName
Specifies the location stack name that this cmdlet makes the current location stack. Enter a
location stack name. To indicate the unnamed default location stack, type $null
or an empty string
("").
The *-Location
cmdlets act on the current stack unless you use the StackName parameter to
specify a different stack.
Type: | String |
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-UseTransaction
Includes the command in the active transaction. This parameter is valid only when a transaction is in progress. For more information, see about_Transactions.
Type: | SwitchParameter |
Aliases: | usetx |
Position: | Named |
Default value: | False |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
You can pipe a string that contains a path, but not a literal path, to this cmdlet.
Outputs
None, System.Management.Automation.PathInfo, System.Management.Automation.PathInfoStack
This cmdlet does not generate any output unless you specify the PassThru parameter. Using PassThru with Path or LiteralPath generates a PathInfo object that represents the new location. Using PassThru with StackName generates a PathInfoStack object representing the new stack context.
Notes
The
Set-Location
cmdlet is designed to work with the data exposed by any provider. To list the providers available in your session, typeGet-PSProvider
. For more information, see about_Providers.A stack is a last-in, first-out list in which only the most recently added item can be accessed. You add items to a stack in the order that you use them, and then retrieve them for use in the reverse order. PowerShell lets you store provider locations in location stacks. PowerShell creates an unnamed default location stack. You can create multiple named location stacks. If you do not specify a stack name, PowerShell uses the current location stack. By default, the unnamed default location is the current location stack, but you can use the
Set-Location
cmdlet to change the current location stack.To manage location stacks, use the
*-Location
cmdlets, as follows:To add a location to a location stack, use the
Push-Location
cmdlet.To get a location from a location stack, use the
Pop-Location
cmdlet.To display the locations in the current location stack, use the Stack parameter of the
Get-Location
cmdlet. To display the locations in a named location stack, use the StackName parameter ofGet-Location
.To create a new location stack, use the StackName parameter of
Push-Location
. If you specify a stack that does not exist,Push-Location
creates the stack.To make a location stack the current location stack, use the StackName parameter of
Set-Location
.The unnamed default location stack is fully accessible only when it is the current location stack. If you make a named location stack the current location stack, you cannot no longer use
Push-Location
orPop-Location
cmdlets add or get items from the default stack or useGet-Location
to display the locations in the unnamed stack. To make the unnamed stack the current stack, use the StackName parameter ofSet-Location
with a value of$null
or an empty string ("").