Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 310 Vote(s) - 3.64 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Determine if PowerShell script has been dot-sourced

#1
From a PowerShell script, how can I determine if the script has been dot-sourced, i.e. it has been called with

. .\myscript.ps1

rather than

.\myscript.ps1

*NOTE* an interesting blog post (also) about this:

[To see links please register here]

Reply

#2
Check $myinvocation.line
It will show the line that was used to call the script.

PS C:\scripts\test> gc test.ps1
$myinvocation.line

PS C:\scripts\test> ./test.ps1
./test.ps1

PS C:\scripts\test> . ./test.ps1
. ./test.ps1

You can also check the .invocationname property. If the script was dot-sourced, it will just be a dot. If not, is will be ./scriptname.ps1
Reply

#3
To complement [mjolinor's helpful answer](

[To see links please register here]

):

**tl;dr**

$isDotSourced = $MyInvocation.InvocationName -eq '.' -or $MyInvocation.Line -eq ''

----------


While `$MyInvocation.InvocationName -eq '.'` _mostly_ tells you whether a given script is being dot-sourced, there is one **exception**:

When you run a script from the - obsolescent<sup>[1]</sup> - **Windows PowerShell ISE** with `Debug > Run/Continue` (<kbd>F5</kbd>), it is _implicitly_ sourced, yet `$MyInvocation.InvocationName` contains the full script filename rather than `.` However, you can detect this case by checking if `$MyInvocation.Line` is _empty_.

(The PIC (PowerShell Integrated Console) that comes with [**Visual Studio Code**](

[To see links please register here]

)'s [PowerShell extension](

[To see links please register here]

) _used to_ behave this way, but as of at least version v2023.1.0 submits explicit dot-sourcing commands).

Note: Detecting whether a _function_ is being dot-sourced is _not_ subject to the exception above, so testing for `$MyInvocation.InvocationName -eq '.'` is sufficient (but the above will work too).

---

<sup>[1] The PowerShell ISE is [no longer actively developed](

[To see links please register here]

) and [there are reasons not to use it](

[To see links please register here]

) (bottom section), notably not being able to run [*PowerShell (Core) 7+*](

[To see links please register here]

). The actively developed, cross-platform editor that offers the best PowerShell development experience is [Visual Studio Code](

[To see links please register here]

) with its [PowerShell extension](

[To see links please register here]

).
</sup>
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through