user1358784
user1358784

Reputation: 115

How to get the current file location in 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")

Upvotes: 2

Views: 16461

Answers (2)

JPBlanc
JPBlanc

Reputation: 72610

$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

Upvotes: 12

David Brabant
David Brabant

Reputation: 43489

$MyInvocation.MyCommand.Path maybe?

Upvotes: 2

Related Questions