powershell-tricks
  • Introduction
  • Run Remote Script from Host
  • Expand Array as Parameter
  • Output a Hashtable
  • Use Add-Type without Pollute Current Session
  • Get-Location is the execution path.
  • About Dot Sourcing
  • About ScriptBlock
Powered by GitBook
On this page

About Dot Sourcing

Use . (dot sourcing) to include file. It will only be included in current scope. This means you cannot write a function to include a file, and use it outside this function. Because it only visible within the function instead of the callilng context.

What you can do is to use another . to open the function also.

# Get-Foo.ps1
function Get-Foo{
  write-host "getting foo"
}
function IncludingFile{
  . "./Get-Foo.ps1"
}
# call this function will do nothing
IncludingFile

# Get-Foo is still not visible here.

This is what you can do.

. IncludingFile

Get-Foo # is visible here.

The base directory of dot sourcing is Get-Location.

PreviousGet-Location is the execution path.NextAbout ScriptBlock

Last updated 7 years ago