Category Archives: Location

Set-Location

NAME
    Set-Location

SYNOPSIS
    Sets the current working location to a specified location.

SYNTAX
    Set-Location [-LiteralPath] <string> [-PassThru] [-UseTransaction] [<CommonParameters>]

    Set-Location [[-Path] <string>] [-PassThru] [-UseTransaction] [<CommonParameters>]

    Set-Location [-StackName <string>] [-PassThru] [-UseTransaction] [<CommonParameters>]

DESCRIPTION
    The Set-Location cmdlet sets the working location to a specified location. That location could be a directory, a sub-directory, a Registry location, or another location stack.

PARAMETERS
    -LiteralPath <string>
        Specifies a path to the location. The value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -PassThru [<SwitchParameter>]
        Passes an object representing the location to the pipeline. By default, this cmdlet does not generate any output.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Path <string>
        This parameter is used to specify the path to a new working location.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? false

    -StackName <string>
        The name of the stack to which the location is being set.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -UseTransaction [<SwitchParameter>]
        Includes the command in the active transaction. This parameter is valid only when a transaction is in progress. For more information, see about_transactions.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.String
        You can pipe a string that contains a path (but not a literal path) to Set-Location.

OUTPUTS
    None or System.Management.Automation.PathInfo
        When you use the PassThru parameter, Set-Location generates a System.Management.Automation.PathInfo object that represents the location. Otherwise, this cmdlet does not generate any output.

NOTES

        The Set-Location cmdlet is designed to work with the data exposed by any provider. To list the providers available in your session, type “Get-PSProvider“. For more information, see about_providers.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Set-Location HKLM:

    Description
    ———–
    This will set the current location to the one specified; in this case, it is the HKLM provider.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Set-Location env: -PassThru

    Path
    —-
    Env:\

    Description
    ———–
    This will set the current location to the one specified; in this case, it is the Environment Variable provider.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Set-Location C:

    Description
    ———–
    This will set the current location to the one specified; in this case, it is the C: drive in the file system provider.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113397
    about_providers
    Get-Location
    Pop-Location
    Push-Location

Pop-Location

NAME
    Pop-Location

SYNOPSIS
    Changes the current location to the location most recently pushed onto the stack. You can pop the location from the default stack or from a stack that you create by using the Push-Location cmdlet.

SYNTAX
    Pop-Location [-PassThru] [-StackName <string>] [-UseTransaction] [<CommonParameters>]

DESCRIPTION
    The Pop-Location cmdlet changes the current location to the location most recently pushed onto the stack by using the Push-Location cmdlet. You can pop a location from the default stack or from a stack that you create by using a Push-Location command.

PARAMETERS
    -PassThru [<SwitchParameter>]
        Passes an object representing the location to the pipeline. By default, this cmdlet does not generate any output.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -StackName <string>
        Specifies an alternate stack. Pop-Location pops the most recently added location from this stack. This stack then becomes the current stack.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -UseTransaction [<SwitchParameter>]
        Includes the command in the active transaction. This parameter is valid only when a transaction is in progress. For more information, see about_transactions.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to Pop-Location.

OUTPUTS
    None or System.Management.Automation.PathInfo
        When you use the PassThru parameter, Pop-Location generates a System.Management.Automation.PathInfo object that represents the location. Otherwise, this cmdlet does not generate any output.

NOTES

        You can also refer to Pop-Location by its built-in Alias, “popd”. For more information, see about_aliases.

        The Pop-Location cmdlet is designed to work with the data exposed by any provider. To list the providers available in your session, type “Get-PSProvider“. For more information, see about_providers.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Pop-Location

    Description
    ———–
    This command changes your location to the location most recently added to the current stack.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Pop-Location -StackName Stack2

    Description
    ———–
    This command changes your location to the location most recently added to the Stack2 stack.

    ————————– EXAMPLE 3 ————————–

    C:\PS>pushd HKLM:\Software\Microsoft\PowerShell

    pushd Cert:\LocalMachine\TrustedPublisher

    popd

    popd

    PS C:\> Push-Location HKLM:\Software\Microsoft\PowerShell

    PS HKLM:\Software\Microsoft\PowerShell> Push-Location Cert:\LocalMachine\TrustedPublisher

    PS cert:\LocalMachine\TrustedPublisher> popd

    PS HKLM:\Software\Microsoft\PowerShell> popd

    PS C:\ps-test>

    Description
    ———–
    These commands use the Push-Location and Pop-Location cmdlets to move between locations supported by different Windows PowerShell providers. The commands use the “pushd” Alias for Push-Location and the “popd” Alias for Pop-Location.

    The first command pushes the current file system location onto the stack and moves to the HKLM drive supported by the Windows PowerShell Registry provider. The second command pushes the Registry location onto the stack and moves to a location supported by the Windows PowerShell Certificate provider.

    The last two commands pop those locations off the stack. The first “popd” command returns to the Registry: drive, and the second command returns to the file system drive.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113369
    about_providers
    Push-Location
    Set-Location
    Get-Location

Push-Location

NAME
    Push-Location

SYNOPSIS
    Adds the current location to the top of a list of locations (a “stack”).

SYNTAX
    Push-Location [[-LiteralPath] <string>] [-PassThru] [-StackName <string>] [-UseTransaction] [<CommonParameters>]

    Push-Location [[-Path] <string>] [-PassThru] [-StackName <string>] [-UseTransaction] [<CommonParameters>]

DESCRIPTION
    The Push-Location cmdlet adds (“pushes”) the current location to the top of a list of locations, called a “stack.” You can push the current location onto a default stack or onto a stack that you create. If you specify a path, Push-Location pushes the current location onto the stack and then changes the current location to the location specified by the path. You cannot push a location onto the stack unless it is the current location.

PARAMETERS
    -LiteralPath <string>
        Specifies the path to the new location. Unlike the Path parameter, the value of the LiteralPath parameter is used exactly as it is typed. No characters are interpreted as wildcards. If the path includes escape characters, enclose it in single quotation marks. Single quotation marks tell Windows PowerShell not to interpret any characters as escape sequences.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -PassThru [<SwitchParameter>]
        Passes an object representing the location to the pipeline. By default, this cmdlet does not generate any output.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Path <string>
        Changes your location to the location specified by this path after it adds (pushes) the current location onto the top of the stack. Enter a path to any location whose provider supports this cmdlet. Wildcards are permitted. The parameter name (“Path”) is optional.

        Required?                    false
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? false

    -StackName <string>
        Specifies the name of a stack. The current location is pushed onto this stack, and this stack becomes the current stack. If the stack does not yet exist, it is created.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -UseTransaction [<SwitchParameter>]
        Includes the command in the active transaction. This parameter is valid only when a transaction is in progress. For more information, see about_transactions.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.String
        You can pipe a string that contains a path (but not a literal path) to Push-Location.

OUTPUTS
    None or System.Management.Automation.PathInfo
        When you use the PassThru parameter, Push-Location generates a System.Management.Automation.PathInfo object that represents the location. Otherwise, this cmdlet does not generate any output.

NOTES

        You can also refer to Push-Location by its built-in Alias, “pushd”. For more information, see about_aliases.

        A “stack” is a “last-in, first-out” (LIFO) data structure. It is like a vertical list in which only the top item is accessible. You can add (“push”) items only to the top of the stack, and you can retrieve (“pop”) items only from the top of the stack. To get to items below the top, you must retrieve all the items above.

        The Push-Location cmdlet is designed to work with the data exposed by any provider. To list the providers available in your session, type “Get-PSProvider“. For more information, see about_providers.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Push-Location C:\Windows

    Description
    ———–
    This command pushes the current location onto the default stack and then changes the location to C:\Windows.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Push-Location HKLM:\Software\Policies -StackName RegFunction

    Description
    ———–
    This command pushes the current location onto the RegFunction stack and changes the current location to the HKLM:\Software\Policies location. This command shows that you can use Push-Location with the Windows PowerShell Registry provider.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Push-Location

    Description
    ———–
    This command pushes the current location onto the default stack.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Push-Location ~ -StackName Stack2

    Description
    ———–
    This command pushes the current location onto a new stack named Stack2 and then changes the current location to the home directory (%USERPROFILE%), which is represented in the command by the tilde symbol (~) or $home. The Stack2 stack then becomes the current stack.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113370
    about_providers
    Pop-Location
    Set-Location
    Get-Location

Get-Location

NAME
    Get-Location

SYNOPSIS
    Gets information about the current working location.

SYNTAX
    Get-Location [-PSDrive <string[]>] [-PSProvider <string[]>] [-UseTransaction] [<CommonParameters>]

    Get-Location [-Stack] [-StackName <string[]>] [-UseTransaction] [<CommonParameters>]

DESCRIPTION
    The Get-Location cmdlet gets an object that represents the current directory, much like the pwd (print working directory) command.

    When you move between Windows PowerShell drives, Windows PowerShell retains your location in each drive. You can use Get-Location to find your location in each drive.

    You can also use Get-Location to get the current directory at run time and use it in Functions and scripts, such as in a Function that displays the current directory in the Windows PowerShell prompt.

    If you use the Push-Location cmdlet to add locations to a path stack, you can use the Stack parameter of Get-Location to display the current stack.

PARAMETERS
    -PSDrive <string[]>
        Gets the current location in the specified Windows PowerShell drive.

        For example, if you are in the Certificate: drive, you can use this parameter to find your current location in the C: drive.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -PSProvider <string[]>
        Gets the current location in the drive supported by the specified Windows PowerShell provider.

        If the specified provider supports more than one drive, Get-Location returns the location on the most recently accessed drive.

        For example, if you are in the C: drive, you can use this parameter to find your current location in the drives of the Windows PowerShell Registry provider.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -Stack [<SwitchParameter>]
        Displays the locations in the default path stack.

        To add paths to the default stack, use the Push-Location cmdlet.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -StackName <string[]>
        Displays the locations in the specified path stacks.

        To create path stacks, use the Push-Location cmdlet.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? false

    -UseTransaction [<SwitchParameter>]
        Includes the command in the active transaction. This parameter is valid only when a transaction is in progress. For more information, see about_transactions.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    None
        You cannot pipe input to this cmdlet.

OUTPUTS
    PathInfo objects or StackInfo objects
        If you use the Stack or StackName parameters, Get-Location returns a StackInfo object. Otherwise, it returns a PathInfo object.

NOTES

        Locations can be stored on a stack. The Push-Location cmdlet adds a location to the top of the stack. The Pop-Location cmdlet gets the location at the top of the stack.

        The ways that the PSProvider, PSDrive, Stack, and StackName parameters interact depends on the provider. Some combinations will result in errors, such as specifying both a drive and a provider that does not expose that drive. If no parameters are specified, Get-Location returns the PathInfo object for the provider that contains the current working location.

        The Get-Location cmdlet is designed to work with the data exposed by any provider. To list the providers available in your session, type “Get-PSProvider“. For more information, see about_providers.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Get-Location

    Path
    —-
    C:\WINDOWS

    Description
    ———–
    This command displays your location in the current Windows PowerShell drive.

    For example, if you are in the Windows directory of the C: drive, it displays the path to that directory.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Set-Location

    Description
    ———–
    These commands demonstrate the use of Get-Location to display your current location in different Windows PowerShell drives.

    The first command uses the Set-Location cmdlet to set the current location to the Windows subdirectory of the C: drive.

        C:\PS> Set-Location C:\Windows

    The second command uses the Set-Location cmdlet to change the location to the HKLM:\Software\Microsoft Registry key. When you change to a location in the HKLM: drive, Windows PowerShell retains your location in the C: drive.

        PS C:\WINDOWS> Set-Location HKLM:\Software\Microsoft
        PS HKLM:\Software\Microsoft>

    The third command uses the Set-Location cmdlet to change the location to the “HKCU:\Control Panel\Input Method” Registry key.

        PS HKLM:\Software\Microsoft> Set-Location ‘HKCU:\Control Panel\Input Method’
        PS HKCU:\Control Panel\Input Method>

    The fourth command uses the Get-Location cmdlet to find the current location on the C: drive. It uses the PSDrive parameter to specify the drive.

        PS HKCU:\Control Panel\Input Method> Get-Location -PSDrive c
        Path
        —-
        C:\WINDOWS

    The fifth command uses the Set-Location cmdlet to return to the C: drive. Even though the command does not specify a subdirectory, Windows PowerShell returns you to the saved location.

        PS HKCU:\Control Panel\Input Method> Set-Location C:
        PS C:\WINDOWS>

    The sixth command uses the Get-Location cmdlet to find the current location in the drives supported by the Windows PowerShell Registry provider. Get-Location returns the location of the most recently accessed Registry drive, HKCU:.

        PS C:\WINDOWS> Get-Location -PSProvider Registry
        Path
        —-
        HKCU:\Control Panel\Input Method

    To see the current location in the HKLM: drive, you need to use the PSDrive parameter to specify the drive. The seventh command does just this:

        PS C:\WINDOWS> Get-Location -PSDrive HKLM
        Path
        —-
        HKLM:\Software\Microsoft

    ————————– EXAMPLE 3 ————————–

    C:\PS>Set-Location

    Description
    ———–
    These commands show how to use the Stack and StackName parameters of Get-Location to list the paths in the default and alternate path stacks.

    The first command sets the current location to the Windows directory on the C: drive.

        C:\PS> Set-Location C:\Windows

    The second command uses the Push-Location cmdlet to push the current location (C:\Windows) onto the path stack and change to the System32 subdirectory. Because no stack is specified, the current location is pushed onto the default stack.
        C:\WINDOWS>Push-Location System32

    The third command pushes the current location (C:\Windows\System32) onto the Stack2 stack and changes the location to the WindowsPowerShell subirectory.

        C:\Windows\System32>Push-Location WindowsPowerShell -stack Stack2

    The fourth command uses the Get-Location cmdlet to get the paths on the default path stack.

        C:\WINDOWS\system32\WindowsPowerShell>Get-Location -stack

        Path
        —-
        C:\WINDOWS

    The last command uses the StackName parameter of Get-Location to get the paths on the Stack2 stack.

        C:\WINDOWS\system32\WindowsPowerShell>Get-Location -stackname Stack2

        Path
        —-
        C:\WINDOWS\system32

    ————————– EXAMPLE 4 ————————–

    C:\PS>function prompt { ‘PowerShell: ‘ + (Get-Location) + ‘> ‘}

    PowerShell: C:\WINDOWS>

    Description
    ———–
    This example shows how to customize the Windows PowerShell prompt. The Function that defines the prompt includes a Get-Location command, which is run whenever the prompt appears in the console.

    The format of the default Windows PowerShell prompt is defined by a special Function called “prompt”. You can change the prompt in your console by creating a new Function called “prompt”.

    To see the current prompt Function, type the following command:

        Get-Content Function:prompt

    The command begins with the “function” keyword followed by the Function name, “prompt”. The Function body appears within braces ( {} ).

    This command defines a new prompt that begins with the string “PowerShell: “. To append the current location, it uses a Get-Location command, which runs when the prompt Function is called. The prompt ends with the string “> “.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=113321
    about_providers
    Pop-Location
    Push-Location
    Set-Location