Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the PowerShell equivalent of bash's exec()?

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?

like image 218
James Ko Avatar asked Dec 25 '15 18:12

James Ko


1 Answers

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

http://i.imgur.com/17xfn6L.png

-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

like image 193
user4317867 Avatar answered Sep 26 '22 19:09

user4317867