How to run powershell inside BAT file from the same directory - powershell

I have a batch script to run powershell, how to set path to ps1 file if this file is in the same folder as executing BAT file? I use this but not working.
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""./Reboot.ps1""' -Verb RunAs}";

Read call /? and Links relative to the Batch Script:
You can get the pathname of the batch script itself with %0,
parameter extensions can be applied to this so %~dp0 will return the
Drive and Path to the batch script.
Use … -File ""%~dp0Reboot.ps1"" … (note that %~dp0 includes a trailing backslash!)

With ./Reboot.ps you address the current working directory. What you need is the directory of the BAT file. Use %~d0%~p0/Reboot.ps instead.
An alternative could be to temporary set the working directory to the BAT's directory:
pushd %~d0%~p%"
rem run script with ./Reboot.ps1
popd
Obviously, if your script "Reboot.ps1" actually reboots the PC, you can skip the popd.

Related

How to open Powershell with cmd and use a .ps1 file

I try to open a ps1 powershell file thats updates a gitrepo
i have tryed
powershell -ExecutionPolicy Bypass -Command "& '%USERPROFILE%\folder1\update.ps1'
this is working but when it comes to a requirements.txt promt writes error of not found that file i think powershell is not inside this folder directly so it cant find that file what is needed
if i make this
cd %USERPROFILE%\ & REM First change to the batch file folder
echo Points as at = %time% %date%> updatebing_log.txt & REM create a simple logfile with time and end result of the batch run
echo ============================folder1=========================== >> updatebing_log.txt
cd folder1\
start powershell
cd %USERPROFILE%\folder1
powershell -ExecutionPolicy Bypass -Command "& '%USERPROFILE%\folder1\update.ps1'
echo Waiting seconds
timeout /t 10 /nobreak > NUL
it is working
but it opens a cmd (bash file) promt and a powershell then it cd to folder
powershell do nothing then
inside cmd i see that the ps1 file is executed and alsow works with the requirements.txt
As per my comment. Here's what your code is doing. Thus the response you're seeing.
# Run this in cmd.exe
cd %USERPROFILE%\ & REM First change to the batch file folder
echo Points as at = %time% %date%> updatebing_log.txt & REM create a simple logfile with time and end result of the batch run
echo ============================folder1=========================== >> updatebing_log.txt
cd folder1\
# Start a PowerShell interactive session
start powershell
cd %USERPROFILE%\folder1
# Start a new PowerShell session from cmd.exe and run this code
powershell -ExecutionPolicy Bypass -Command "& "$env:USERPROFILE\folder1\update.ps1" # note the profile change I made for you.
# Run this in cmd.exe on Powerhell exit
echo Waiting seconds
timeout /t 10 /nobreak > NUL
If that is not the flow you were after, then you need to rethink this.
i made this at work it looks likke it works but dont know i try to show the working directory but it want show it only shows were the ps1 call file is
i made a test.bat
powershell -ExecutionPolicy Bypass -Command "& '%USERPROFILE%\Brewd\updatebrewdall.ps1'"
and updatecall.ps1
cd "folder1\"
powershell -ExecutionPolicy Bypass -Command "& '.\update.ps1'"
Start-Sleep -Seconds 10
# now back to previous directory
cd ..\
cd "folder2\"
"$([Environment]::CurrentDirectory)\$ComputerName"
powershell -ExecutionPolicy Bypass -Command "& '.\update.ps1'"
Start-Sleep -Seconds 10
# now back to previous directory
cd ..\
cd "folder3\"
"$([Environment]::CurrentDirectory)\$ComputerName"
powershell -ExecutionPolicy Bypass -Command "& '.\update.ps1'"
Start-Sleep -Seconds 10

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.

Batch file to run PowerShell Script Only Works Once

So I'm trying to create a batch file to run a PowerShell script while bypassing the execution policy. Oddly, it worked a single time, but without me editing anything, it will not run again. I've created other files thinking maybe my file somehow got corrupted, but nothing... Any chance someone sees anything blatantly wrong with this?
#echo off
Powershell.exe -Command "& {Start-Process Powershell.exe -ArgumentList '-ExecutionPolicy Bypass -File %~dp0File.ps1' -Verb RunAs}"
PAUSE
The *.ps1 file works by itself if I click through the prompts. Also, if I manually set the execution policy in PowerShell to Bypass, this batch file still does not work. This is not a process I usually need to take, so I'm curious if anyone sees anything wrong with how this is written?
If this is just to run your script, what I personally do is create a shortcut of the script and then modify the Target of the shortcut:
Target: Powershell.exe -ExecutionPolicy Bypass -File "C:\scriptpath\script.ps1"
If you want your script to be executed as Administrator you can add this to the top of the main script:
$myInvoke="-file `"$($MyInvocation.ScriptName)`""
Start-Process "$PSHome\powershell.exe" -Verb Runas -ArgumentList $myInvoke -EA 'Stop'
If the shortcut will always be in the same folder as your script you can also leave Start In blank and change the path for Powershell.exe -ExecutionPolicy Bypass -File ".\script.ps1" by doing so if you copy the entire folder to a different location, the shortcut will still work.

running watch file in to schedule

Have a requirement of using batch file in Cognos to upload the output in SharePoint by using Windows batch file we are scheduling.
Here is my script:
#ECHO OFF
IF EXIST "D:\Dev\close report name-en-in" Call Powershell.exe -executionpolicy remotesigned -File 'D:\Dev\close\pwrshell.ps1'
GOTO END
:END
PowerShell script is used for uploading the script from source.
After running batch file the output is not uploaded to SharePoint (PowerShell script works fine when running alone).
Not sure if this works or not, but there is a simple error in your script.
Call Powershell.exe -executionpolicy remotesigned -File 'D:\Dev\close\pwrshell.ps1'
should be
Powershell.exe -executionpolicy remotesigned -File 'D:\Dev\close\pwrshell.ps1'
Here's some better ways to end the file.
exit /b [errorlevel]
This stops the script, and optionally set an errorlevel.
goto :eof
We don't need :EOF for this to work, goto :eof works standalone.

Running a PS1 script using a batch file (.bat)

Currently the following is my path for launching the VMware vSphere PowerCLI command prompt. I wish to run my sample.ps1 script automatically using a batch file. How can I incoporate sample.ps1 into this path and create a batch file?
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\""
If you are working with PowerShell 2.0, you can use the -file parameter of PowerShell.exe
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -file "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1"
If you are working with PowerShell 1.0, you can use -command parameter this way
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -noe -command "& 'C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1'"
echo off
Title,Report Script &color 9e
for /f "usebackq delims=$" %%a in (`cd`) do (
set SCRIPTDIR=%%a
)
(Set ScriptFile=%SCRIPTDIR%\Report.ps1)
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -psc "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" -c ". \"C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1\";%ScriptFile%"
You can use this to launch arbitrary .ps1 scripts via .bat files by calling the bat file like your ps1. Then extract the name of file in batch and call powershell with it.
For a ready to use solution, use the following Gist: https://gist.github.com/JonasGroeger/10417237
I saw this code in another page, I test it in a W2012 R2 and it runs.
I hope it work:
C:\>powershell "C:\>1\file.ps1"