How to launch PowerShell script from the OS command line? - windows

I have a PowerShell script for building my project files, and I'd like to have capability to run it from my file manager's command line (and, possibly, make a shortcut to this script so I can start build from my desktop)
Any way to do this?

If you're on PowerShell 2.0 use:
PowerShell.exe -File c:\users\john\myscript.ps1
If you're on 1.0 use:
PowerShell -Command "& {c:\users\john\myscript.ps1}"
Depending on what you do/load in your profile script you may also want to specify -NoProfile. Of course, if your script requires something that is loaded in your profile then don't use this parameter. Otherwise, it can speed up execution of your script a bit.

invoke-command -computername -scriptblock{param()} -ArgumentList

Related

How to start git-bash with some argument on separate new window from Powershell

I am trying to do the following steps from Powershell. Following is just a pseudo-code which explains my requirements.
# This is a powershell function
function load(){
cd C:\my_path\scripts
invoke-expression -Command C:\Program Files\Git\git-bash.exe
# I want to go to this path into the git-bash.exe window
cd C:\my_path\scripts
# I have bash script here. I want to excuete this script.
./loadData.sh
}
how can I achieve this by Powershell? Thanks!
try git-bash.exe --help to see what parameters it has. after a short google search i assume you can run Start-Process -FilePath "C:\Program Files\Git\git-bash.exe" -ArgumentList '--cd="C:\my_path\scripts" --exec="loadData.sh"' maybe even Start-Process -FilePath "C:\Program Files\Git\git-bash.exe" -ArgumentList '--exec="C:\my_path\scripts\loadData.sh"' might work.
If you simply want to execute the bash script loadData.sh, run :
cd C:\my_path\scripts
path/to/bash.exe loadData.sh
This will create a bash shell (in the current console, not in a separate one), run your script, and exit, returning to your powershell.
If the current working directory is not set as you expect within your bash shell, you can perhaps pass it as an argument to your script :
# not 100% tested, I don't have a Powershell at hand
path/to/bash.exe loadData.sh /c/my_path/scripts
# and in your loadData.sh, add an instruction :
cd "$1"

How to "open with" in a batch file

I have a windows powershell script that will be available on a server to my users. I don't want them to have to go out and find the PS script, right click and click "run with powershell" or do an "open with". The Windows (Win 7 at least) default program is notepad.
I want to make a batch file to do this. I've tried:
start "c:\myfile.ps1" powershell.exe
and a few other variations, but all I've been able to do is either start powershell, or open my file in its default program, notepad.
Any advice is appreciated! Thanks!
Bonus question: If I run my batch file as administrator will it also run my PS script as administrator?
Simply use the -file argument for PowerShell.exe in your batch file:
PowerShell.exe -file c:\MyFile.ps1
Additionally, some users may have their Execution Policy set to something that would restrict scripts from being executed, so you may want to do something like:
PowerShell.exe -ExecutionPolicy Bypass -file c:\MyFile.ps1
If you would like to use start to launch it you can do so as Ansgar Wiechers noted by running:
start "" PowerShell.exe -ExecutionPolicy Bypass -file c:\MyFile.ps1
Some notes regarding using start: By default it will launch PowerShell in a separate window, and continue to execute the rest of the batch file without waiting for the PowerShell window to close. If that is undesirable you have two options. You can specify /wait which will wait for the PowerShell window to close before continuing the batch file, or you can use the /B option will will not open a new window, and will execute PowerShell in the current console window.
And finally, yes if your batch file is run under the Administrator context, PowerShell will be as well.

Launching from .bat not working the same as running .ps1 directly

I'm trying to make a script that people can doubleclick to run a set of commands that changes occasionally. In linux I run a .sh that wgets another sh (that I can change when needed) and runs it. I'm looking for a similar solution for Windows.
I looked into PowerShell but found out it needs to be run from a .bat to allow double clicking, so now I have the following:
foo.bat file:
#ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%bar.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}";
that calls bar.ps1:
Invoke-WebRequest https://dl.dropboxusercontent.com/u/xxx/bar2.ps1 -OutFile bar2.ps1
Running bar.ps1 from PowerShell works fine and downloads bar2.ps1, but running the batch file does not, or at least not to the directory both files are in (can't find it anywhere, though I guess it might be a working directory problem still?).
Your batch file is running the PowerShell script with elevated privileges. Doing this changes the working directory to C:\Windows\system32 (for security reasons), so you'll most likely find the downloaded file there. To get the ouptut file in the same directory as the script you could change the PowerShell script like this:
$dir = Split-Path $MyInvocation.MyCommand.Path -Parent
$filename = 'bar2.ps1'
$url = "https://dl.dropboxusercontent.com/u/xxx/$filename"
Invoke-WebRequest $url -OutFile "$dir/$filename"
With that said, why are you running a download script with elevated permissions in the first place? Don't do that.

How to call cmd batch from within powershell script

I have
C:\folder\tail.exe
C:\logs\logfile.log
C:\script\shellscript.ps1
How do I run the C:\folder\tail.exe from within the C:\script\shellscript.ps1
I need to run "C:\folder\tailf.exe C:\logs\logfile.log" from within C:\script\shellscript.ps1 but without relying a seperate batch file, i need to call it directly.
Normally I do: cd C:\folder\ and then tailf.exe C:\logs\logfile.log
Inside C:\script\shellscript.ps1 I tried
start-process C:\fetchmail\tail.exe -argumentlist "C:\fetchmail\logs\fetchmail.log"
i can see a window flashing but dont know if it works, the window should stay open.
You can do what you normally do:
C:\folder\tail.exe c:\logs\logfile.log
Note that if the paths have spaces in them you have to do:
& "C:\fol der\tail.exe" "c:\log s\logfile.log"
To accomplish tail.exe running in a separate window and not having the window closed immediately, try:
cmd /k c:\folder\tail.exe c:\logs\test.log
To start tail.exe in the powershell window, try:
C:\folder\tail.exe c:\logs\logfile.log
Assuming tail.exe is Tail for Win32, this is a powershell equivalent of your command:
get-content c:\logs\logfile.log | select -last 10
In the Powershell Community Extensions there is a Get-FileTail cmdlet which is a more efficient native powershell tail equivalent.

How to run a PowerShell script

How do I run a PowerShell script?
I have a script named myscript.ps1
I have all the necessary frameworks installed
I set that execution policy thing
I have followed the instructions on this MSDN help page
and am trying to run it like so:
powershell.exe 'C:\my_path\yada_yada\run_import_script.ps1' (with or without --noexit)
which returns exactly nothing, except that the file name is output.
No error, no message, nothing. Oh, when I add -noexit, the same thing happens, but I remain within PowerShell and have to exit manually.
The .ps1 file is supposed to run a program and return the error level dependent on that program's output. But I'm quite sure I'm not even getting there yet.
What am I doing wrong?
Prerequisites:
You need to be able to run PowerShell as an administrator
You need to set your PowerShell execution policy to a permissive value or be able to bypass it
Steps:
Launch Windows PowerShell as an Administrator, and wait for the PS> prompt to appear
Navigate within PowerShell to the directory where the script lives:
PS> cd C:\my_path\yada_yada\ (enter)
Execute the script:
PS> .\run_import_script.ps1 (enter)
Or: you can run the PowerShell script from the Command Prompt (cmd.exe) like this:
powershell -noexit "& ""C:\my_path\yada_yada\run_import_script.ps1""" (enter)
according to Invoking a PowerShell script from cmd.exe (or Start | Run) by Kirk Munro.
Or you could even run your PowerShell script asynchronously from your C# application.
If you are on PowerShell 2.0, use PowerShell.exe's -File parameter to invoke a script from another environment, like cmd.exe. For example:
Powershell.exe -File C:\my_path\yada_yada\run_import_script.ps1
If you want to run a script without modifying the default script execution policy, you can use the bypass switch when launching Windows PowerShell.
powershell [-noexit] -executionpolicy bypass -File <Filename>
Type:
powershell -executionpolicy bypass -File .\Test.ps1
NOTE: Here Test.ps1 is the PowerShell script.
I've had the same problem, and I tried and tried... Finally I used:
powershell.exe -noexit "& 'c:\Data\ScheduledScripts\ShutdownVM.ps1'"
And put this line in a batch-file, and this works.
If you only have PowerShell 1.0, this seems to do the trick well enough:
powershell -command - < c:\mypath\myscript.ps1
It pipes the script file to the PowerShell command line.
Pretty easy. Right click the .ps1 file in Windows and in the shell menu click on Run with PowerShell.
Open PowerShell in administrator mode
Run: set-executionpolicy unrestricted
Open a regular PowerShell window and run your script.
I found this solution following the link that was given as part of the error message: About Execution Policies
Make sure to run set-ExecutionPolicy default once you're done, or you will be exposed to security risks.
Using cmd (BAT) file:
#echo off
color 1F
echo.
C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "PrepareEnvironment.ps1"
:EOF
echo Waiting seconds
timeout /t 10 /nobreak > NUL
If you need run as administrator:
Make a shortcut pointed to the command prompt (I named it
Administrative Command Prompt)
Open the shortcut's properties and go to the Compatibility tab
Under the Privilege Level section, make sure the checkbox next to "Run this program as an administrator" is checked
An easy way is to use PowerShell ISE, open script, run and invoke your script, function...
In case you want to run a PowerShell script with Windows Task Scheduler, please follow the steps below:
Create a task
Set Program/Script to Powershell.exe
Set Arguments to -File "C:\xxx.ps1"
It's from another answer, How do I execute a PowerShell script automatically using Windows task scheduler?.
If your script is named with the .ps1 extension and you're in a PowerShell window, you just run ./myscript.ps1 (assuming the file is in your working directory).
This is true for me anyway on Windows 10 with PowerShell version 5.1 anyway, and I don't think I've done anything to make it possible.
Give the path of the script, that is, path setting by cmd:
$> . c:\program file\prog.ps1
Run the entry point function of PowerShell:
For example, $> add or entry_func or main
You can run from cmd like this:
type "script_path" | powershell.exe -c -
Use the -File parameter in front of the filename. The quotes make PowerShell think it is a string of commands.
I've just found the method what Microsoft do when we right click on a ps1 script and click on "Run with PowerShell" :
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & 'C:\Users\USERNAME\Desktop\MYSCRIPT.ps1'"
With the appropriate execution policy, you should just be able to call the file directly and Windows will associate it with PowerShell
C:\my_path\yada_yada\run_import_script.ps1
That does not do so well with arguments. The real answer to your question is that you are missing the & to say "execute this"
powershell.exe '& C:\my_path\yada_yada\run_import_script.ps1'

Resources