Newbie to PowerShell here. What's the best way to run a new script from within the current process without creating a subshell? In Bash, you would do this via:
source script # Executes the commands in script within the current shell
or
exec script # Same as source, except it completely replaces the current process with script
Is there an equivalent for this in PowerShell?
You can also use .\Script.ps1
Or start typing the name and use TAB
to complete the script name.
Dot-sourcing a script allows a function to become available on the command line.
For example:
Function Get-LocalTime {
$CurTime = Get-Date
"The Date and time currently is $CurTime"
}
Now we dot source (name the above script Example.ps1) PS C:\>. .\Example.ps1
Allowing us to simply type and Tab complete Get-LocalTime
-edit to comment, you can define a function and use the function immediately in the same script. So in Example.ps1
, at the last line just enter Get-LocalTime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With