Executing Powershell script from command prompt without changing environment? - powershell

I am writing a power shell script to execute one sample script. But when i am running it from command prompt it is changing environment from command prompt to Power shell and executing script. Which i don't want .
Powershell.exe -executionpolicy remotesigned -File C:\Users\ic020511\Desktop\Script.ps1
Is there any command that would not switch from command prompt and execute script

You should be able to do that using the start command:
start Powershell.exe -windowstyle hidden -executionpolicy remotesigned -File C:\Users\ic020511\Desktop\Script.ps1

Related

Running a powershell script as administrator and minimized

So I have set up a task on task scheduler to run a .bat file that runs a powershell script as admin which sets the DNS settings. I figured out how to make the .bat file run minimised, but the powershell window still pops up. Here is the script for the .bat file called "SetDNS". The powershell script's name is "DNS.ps1".
#ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%DNS.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%C:\Users\Test\Downloads\DNS.ps1%""' -Verb RunAs}";
I want to change it so that the powershell script does not flash open while it runs. Is there something that I could add to the above code to make it run minimized? I tried to add "start /min" to the above code but it did not work. Help is appreciated.

Calling a Powershell script from Batch script..But need the batch script to wait for the Powershell script to complete

I have a batch script being that has the following code in it:
Here is the batch script (Test.bat):
call powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1
exit /b
The Powershell script (TheScriptToBeExecuted.ps1) has some wait built into it. But when this file is being called, the batch script is NOT waiting for the Powershell script to finish.
I ever tried:
START /wait powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1
To no effect.
When I double click on the Test.bat file, it seems to be waiting. I have checked out a lot of answers, but couldnt find any that correspond to this issue.
Please help.
start /wait powershell.exe -executionpolicy remotesigned -File TheScriptToBeExecuted.ps1 | ECHO > nul
Try the above. Holds the command until scripts executed, without the Teminate Batch Prompt. Use start /min /wait if not needing to display or interact with the script, or Alternately:
start /wait powershell.exe -executionpolicy remotesigned -WindowStyle Hidden -File TheScriptToBeExecuted.ps1 | ECHO > nul
-WindowStyle Value
Sets the window style for the session. Valid values are Normal, Minimized, Maximized and Hidden.
* | * pipes the output of the command, however as your just starting powershell.exe and not using it in the context of utilizing a command like follows, It shouldn't be an issue.
powershell -command "((Get-date).AddDays(0)).ToString(':dd:MM:yyyy') | set-content 'captureVar.txt'" && set /p Yesterday=<captureVar.txt
It worked for the script I tested, but it's not thoroughly tested.
let me know how it goes.
I think the issue is more likely to be within "TheScriptToBeExecuted.ps1" than your bat. In fact several powershell command are working as Asyncronous.
I tested a Sleep-Start, which is not Asyncronous at all using the following code and the bat is waiting the end of it.
This is the code I used in my .bat
REM RUNTEST.BAT
time /T
echo started
powershell -executionpolicy remotesigned -File C:\Users\Alex\Desktop\DA_PROVARE\test.ps1
time /T
echo done
exit /b
The ps1 content is just one row, here below:
Start-Sleep -Seconds 25
The problem you are facing is probably because of the account under which the scheduler runs. Have faced similar problems where the .bat and/or .ps1 doesn't behave the same if launched by a scheduler or other tools
powershell -ExecutionPolicy ByPass -command ". 'C:\path\TheScriptToBeExecuted.ps1'"
This should do the it, if not then we will add < NUL to the end
powershell -ExecutionPolicy ByPass -command ". 'C:\path\TheScriptToBeExecuted.ps1'" < NUL
By using dot sourcing, we are opening powershell in the cmd window with the powershell reference in the above solution. Then the period allows the script to run in cmd window also. I did a double test and made a .ps1 with start-sleep -s 3 in it, and the above did wait for the powershell script to finish.
method 2
Using Start /wait
START /wait PowerShell.exe "& "'C:\path\TheScriptToBeExecuted.ps1'"
This method will not run inside the cmd window. It opens a powershell terminal and runs there then goes back to the bat.

Windows PowerShell to install NativeScript on Windows 10

I try to install NativeScript on Windows 10 from Admin PowerShell console.
https://docs.nativescript.org/start/ns-setup-win
I type this command and obtain the following error:
#powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://nativescript.org/setup/win-avd'))"
You're running the command from PowerShell when you need to run it from CMD command prompt.
Run this from CMD:
#powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://nativescript.org/setup/win-avd'))"
Here, #powershell means "don't echo the command and run powershell.exe". It means something completely different in PowerShell itself, where #powershell means "search the path and execute a program with the literal name #powershell before the extension" or "use the variable $powershell as a splat variable" depending on your PowerShell version.
If you're running it in Powershell, you'll need to run just:
iex ((new-object net.webclient).DownloadString('https://nativescript.org/setup/win-avd'))
As always, be extremely careful about running these commands that download arbitrary code and execute it immediately.

How can I make a .bat file not exit when I run a powershell script?

I put this code into a .bat script to run a file:
powershell -NoProfile -ExecutionPolicy Bypass .\abc.ps1"
When the script finishes the window closes and I can't see the output. How can I make the window stay open?
Depends on what you want left in the window. Do you want a command prompt? If so use:
cmd /k powershell -NoProfile -ExecutionPolicy Bypass .\abc.ps1
Do you want a powershell window?
powershell -NoExit -NoProfile -ExecutionPolicy Bypass .\abc.ps1

How do I set up PowerShell as my SmartGit "Open Git-Shell" command?

I have tried setting up PowerShell as my "Open Git-Shell" option with no success. In the Command field I have set the following options:
Command:
powershell.exe
Arguments:
-noexit -executionpolicy Unrestricted -command "cd '${filePath}'"
This will launch PowerShell in non-interactive mode (i.e.: the process is running in task manager, but the UI isn't visible). What am I doing wrong?
I played around with using cmd.exe to invoke PowerShell for "Open Git-Shell" in SmartGit. I was able to get the desired result with the following:
Command:
cmd.exe
Arguments:
/c start powershell.exe -noexit -executionpolicy Unrestricted -command "cd '${filePath}'"