Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current file location in PowerShell

Tags:

powershell

I am wondering how to get the location of the script I am running or writing. We have a set of .NET assemblies placed in the same directory as the current PowerShell script, and we want load them from the script. Right now I am using a fixed path to locate assemblies, but we want to load them from the current file location.

[System.Reflection.Assembly]::LoadFrom
("C:\Work\Scripts\Assemblies\DynamicOps.ManagementModel.Client.dll")

[System.Reflection.Assembly]::LoadFrom
("C:\Work\Scripts\Assemblies\DynamicOps.Repository.dll")
like image 815
user1358784 Avatar asked Nov 21 '25 17:11

user1358784


1 Answers

$MyInvocation

Contains an object with information about the current command, such as a script, function, or script block. You can use the information in the object, such as the path and file name of the script ($myinvocation.mycommand.path) or the name of a function ($myinvocation.mycommand.name) to identify the current command. This is particularly useful for finding the name of the script that is running. You'll get full help on automatic varible with about_Automatic_Variables :

get-help about_Automatic_Variables -full

(Edited) To get current directory you've got the CmdLet Get-Location

like image 66
JPBlanc Avatar answered Nov 23 '25 16:11

JPBlanc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!