So if I have a directory stored in a variable, say:
$scriptPath = (Get-ScriptDirectory); Now I would like to find the directory two parent levels up.
I need a nice way of doing:
$parentPath = Split-Path -parent $scriptPath $rootPath = Split-Path -parent $parentPath Can I get to the rootPath in one line of code?
You can go back to the parent directory of any current directory by using the command cd .. , as the full path of the current working directory is understood by Bash . You can also go back to your home directory (e.g. /users/jpalomino ) at any time using the command cd ~ (the character known as the tilde).
PowerShell Get Current Directory of Script File To get current directory of script file or running script, use $PSScriptRoot automatic variable. PSScriptRoot variable contains full script to path which invoke the current command.
The Windows PowerShell prompt opens by default at the root of your user folder. Change to the root of C:\ by entering cd c:\ inside the Windows PowerShell prompt.
get-item is your friendly helping hand here.
(get-item $scriptPath ).parent.parent If you Want the string only
(get-item $scriptPath ).parent.parent.FullName If $scriptPath points to a file then you have to call Directory property on it first, so the call would look like this
(get-item $scriptPath).Directory.Parent.Parent.FullName Remarks
This will only work if $scriptPath exists. Otherwise you have to use Split-Path cmdlet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With