0

I would like to execute a program.exe which resides next to the script.ps1 file... How can i get this location/folder? (this is not a fixed location/folder, can be a usb stick or network drive etc)

I have tried to use

$MyInvocation.MyCommand.Path

But it seems not to be helping me (or i'm not using it the rigth way)

Thanks

Nomistake
  • 893
  • 2
  • 17
  • 32
  • dupe http://stackoverflow.com/questions/5466329/whats-the-best-way-to-determine-the-location-of-the-current-powershell-script – Andy Arismendi Apr 26 '13 at 14:53

1 Answers1

3

This is the pattern I normally use:

$exeName        = "MyApplication.exe"
$scriptFolder   = Split-Path -Parent $MyInvocation.MyCommand.Path
$exeFullPath    = Join-Path -Path $scriptFolder -ChildPath $exeName

$MyInvocation is an automatic variable.

Contains an information about the current command, such as the name, parameters, parameter values, and information about how the command was started, called, or "invoked," such as the name of the script that called the current command.

Note that the object returned by $MyInvocation.MyCommand is different depending upon the context from which it was executed.

The type ScriptInfo is returned from the powershell command window, notice the lack of Path property:

   TypeName: System.Management.Automation.ScriptInfo

Name          MemberType     Definition
----          ----------     ----------
Equals        Method         bool Equals(System.Object obj)
GetHashCode   Method         int GetHashCode()
GetType       Method         type GetType()
ToString      Method         string ToString()
CommandType   Property       System.Management.Automation.CommandTypes CommandType {get;}
Definition    Property       System.String Definition {get;}
Module        Property       System.Management.Automation.PSModuleInfo Module {get;}
ModuleName    Property       System.String ModuleName {get;}
Name          Property       System.String Name {get;}
OutputType    Property       System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.PSTyp...
Parameters    Property       System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Cult...
ParameterSets Property       System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.Comma...
ScriptBlock   Property       System.Management.Automation.ScriptBlock ScriptBlock {get;}
Visibility    Property       System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
HelpUri       ScriptProperty System.Object HelpUri {get=try...

Whereas the type is ExternalScriptInfo when run from a script, notice the additional properties ScriptContents and Path etc.

   TypeName: System.Management.Automation.ExternalScriptInfo

Name             MemberType     Definition
----             ----------     ----------
Equals           Method         bool Equals(System.Object obj)
GetHashCode      Method         int GetHashCode()
GetType          Method         type GetType()
ToString         Method         string ToString()
CommandType      Property       System.Management.Automation.CommandTypes CommandType {get;}
Definition       Property       System.String Definition {get;}
Module           Property       System.Management.Automation.PSModuleInfo Module {get;}
ModuleName       Property       System.String ModuleName {get;}
Name             Property       System.String Name {get;}
OriginalEncoding Property       System.Text.Encoding OriginalEncoding {get;}
OutputType       Property       System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.PS...
Parameters       Property       System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, C...
ParameterSets    Property       System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Management.Automation.Co...
Path             Property       System.String Path {get;}
ScriptBlock      Property       System.Management.Automation.ScriptBlock ScriptBlock {get;}
ScriptContents   Property       System.String ScriptContents {get;}
Visibility       Property       System.Management.Automation.SessionStateEntryVisibility Visibility {get;set;}
HelpUri          ScriptProperty System.Object HelpUri {get=try...
David Martin
  • 11,764
  • 1
  • 61
  • 74
  • what does $MyInvocation stand for? – Nomistake Apr 26 '13 at 12:01
  • when i execute this (after replacing the $execName's value) i get erros on: Split-Path : Cannot bind argument to parameter 'Path' because it is null. Join-Path : Cannot bind argument to parameter 'Path' because it is null. any idea? – Nomistake Apr 26 '13 at 12:23
  • Are you running this from PowerGui? Try running it from the powershell command line or calling it from another script. - http://stackoverflow.com/questions/2989069/myinvocation-mycommand-path-is-null-in-powergui-script-editor – David Martin Apr 26 '13 at 12:27
  • i have tried it from the command and also from the ISE the property path seems to be null... – Nomistake Apr 26 '13 at 12:38
  • It shouldn't be null, try this from the powershell command prompt (it will create a file named test.ps1): ("`$MyInvocation.MyCommand.Path" | Out-File test.ps1);.\test.ps1 – David Martin Apr 26 '13 at 12:43
  • it has something to do with that Split-path? – Nomistake Apr 26 '13 at 12:52
  • How are you running your script, is it being called from a module or another script? it will be null if you try it from the command line as it needs to be in the scope of a script. – David Martin Apr 26 '13 at 12:54
  • straight in the command window. I'm not calling a script `$scriptFolder = Split-Path -Parent $MyInvocation.MyCommand.Path` gives: _Split-Path : Cannot bind argument to parameter 'Path' because it is null._ – Nomistake Apr 26 '13 at 12:56
  • It needs to be in a .ps1 file, like my example (see test.ps1) – David Martin Apr 26 '13 at 12:59
  • Well, i am even also trying from de ISE editor and running this little script with F5, but this gives the same error (script saved as a ps1) – Nomistake Apr 26 '13 at 13:05
  • calling the script also no succes – Nomistake Apr 26 '13 at 13:05
  • if i `$MyInvocation.MyCommand | Get-Member` i dont see the property path – Nomistake Apr 26 '13 at 13:09
  • If you run it from the command you will get a different object (System.Management.Automation.ScriptInfo) when you run it from a script you will get (System.Management.Automation.ExternalScriptInfo) the latter has Path. That's why it needs to be in a PS1 file and why the test.ps1 worked. – David Martin Apr 26 '13 at 13:17
  • i got i working. like you said, i had to call the script file from the run or from in the command and it works now. It work now even from within the editor. Don't know what went wrong... but it works :-) Thank you for your advices. – Nomistake Apr 26 '13 at 13:39
  • could the fact that is was in a rdp session before made a difference? – Nomistake Apr 26 '13 at 13:41