Reputation: 559
I'm using PowerShell
Ver. 5.1.14393.2248
My script is located in the path: C:\Anwendungen\PowershellAddOn\Modules\PSScriptVersionChecker
.
I want to get the parent path like: C:\Anwendungen\PowershellAddOn\Modules
.
When I run:
$destination = Split-Path -Path $PSCommandPath -Parent
$destination
I'll still get C:\Anwendungen\PowershellAddOn\Modules\PSScriptVersionChecker
instead of C:\Anwendungen\PowershellAddOn\Modules
What am I doing wrong?
Upvotes: 2
Views: 5945
Reputation: 30153
Read about_Automatic_Variables:
$PSCommandPath Contains the full path and file name of the script that is being run. This variable is valid in all scripts. … $PSScriptRoot Contains the directory from which a script is being run. In Windows PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in Windows PowerShell 3.0, it is valid in all scripts.
Use:
$destination = Split-Path -Path (Split-Path -Path $PSCommandPath -Parent) -Parent
or, in Windows PowerShell 3.0 and above:
$destination = Split-Path -Path $PSScriptRoot -Parent
Upvotes: 1
Reputation: 58991
You should check the output of $PSCommandPath when running the script. It probably don't point to C:\Anwendungen\PowershellAddOn\Modules\PSScriptVersionChecker
since when you invoke:
Split-Path C:\Anwendungen\PowershellAddOn\Modules\PSScriptVersionChecker
You will get C:\Anwendungen\PowershellAddOn\Modules
.
Upvotes: 1