Powershell

What’s the Best Way to Determine the Location of the Current PowerShell Script?

PowerShell is a scripting language mostly used to perform automation tasks on the system. It has a host application called “PowerShell ISE”, used to create and execute scripts and functions. The PowerShell scripts are saved with the “.ps1” extension. The location of the PowerShell scripts can be determined by executing specific commands in PowerShell ISE within a script. More specifically, determining a PowerShell script location helps in referring to a relative script.

This post will exhibit multiple methods to resolve the mentioned query.

What’s the Best/Better Way/Method to Determine the Location/Path of the Current PowerShell Script?

These methods can be employed to resolve the mentioned query:

Method 1: Use the “$PSScriptRoot” to Determine/Discover the Location/Path of the Script in PowerShell

PowerShell’s script location can be determined by executing the “$PSScriptRoot” command inside a script. It is specially designed to fetch the location of the PowerShell script. For instance, have a look at the given demonstration mentioned below.

Example

Execute the given command in the PowerShell ISE console to get/fetch the location/path of the PowerShell script:

> $PSScriptRoot

It can be observed that the location/path of the script has been determined.

Method 2: Use the “$PSCommandPath” to Determine/Discover the Location/Path of PowerShell Script

The “$PSCommandPath” cmdlet not only determines the current location/path of the PowerShell script. Moreover, it also displays the name of the script.

Example

Run the given command to fetch the location of the PowerShell script:

> $PSCommandPath

The PowerShell script location and its name have been successfully displayed in the output.

Method 3: Use the “Split-Path” to Determine/Discover the Location/Path of the Script in PowerShell

Another method that can print out the location of the PowerShell script is a “Split-Path”. It is utilized to get the part of the given path, full path, or file name.

Example

In order to get/retrieve the current path of the script, just execute the mentioned command in the PowerShell ISE’s script:

> Split-Path $psISE.CurrentFile.FullPath

The current location of the PowerShell script has been printed out.

Conclusion

The location of the current PowerShell script can be determined using several methods. These methods include “$PSScriptRoot”, “$PSCommandPath”, and “Split-Path”. Whenever each of these three methods gets executed, they output the current location of the PowerShell script. This post has provided several methods to determine the current location of the PowerShell script.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.