Coding Ref

How to get the current directory in PowerShell

How to get the current directory in PowerShell

Using Resolve-Path

To get the full path of the current directory in PowerShell, you can use the Resolve-Path cmdlet. This cmdlet allows you to resolve the path of a file or directory to its full, absolute path.

Here is an example of how you can use the Resolve-Path cmdlet to get the full path of the current directory in PowerShell:

# Get the full path of the current directory
$currentPath = Resolve-Path .

# Print the full path
Write-Host "The full path of the current directory is: $currentPath"

In the example above, the Resolve-Path cmdlet is used to resolve the path of the current directory (.) to its full, absolute path. The resulting path is stored in the $currentPath variable, and then printed to the PowerShell console using the Write-Host cmdlet.

Using Split-Path

Alternatively, you can use the Split-Path cmdlet to get the full path of the current directory.

Here is an example of how you can use the Split-Path cmdlet to get the full path of the current directory:

# Get the full path of the current directory
$currentPath = Split-Path -Path . -Resolve

# Print the full path
Write-Host "The full path of the current directory is: $currentPath"

In the example above, the Split-Path cmdlet is used to split the path of the current directory (.) into its parent directory and the current directory itself. The -Resolve parameter is used to resolve the resulting path to its full, absolute path. The resulting path is stored in the $currentPath variable, and then printed to the PowerShell console using the Write-Host cmdlet.

Conclusion

Either of these methods can be used to get the full path of the current directory in PowerShell. You can use this information to access or manipulate files and directories in the current directory, or to perform other operations that require the full path of the current directory.

You'll also like

Related tutorials curated for you

    How to count objects in PowerShell

    What is a Scriptblock in PowerShell?

    PowerShell do-while loop

    What are the different PowerShell file types?

    What is PowerShell ExpandProperty?

    How to add a user to a group in PowerShell

    What is null in PowerShell?

    How to use PowerShell exit codes

    Dictionaries in PowerShell

    Not equal to in PowerShell

    How to append content to a file in PowerShell

    What are classes in PowerShell?