How can I run a powershell script from startup/autostart folder with bat file? - powershell

Are those files put into a cmd context and then just executed/typed ?
A .bat file is just run, but a powershell file can`t just be run.
How can I make it run without bat file?

You don't need a batch file (.bat or .cmd file). Just create a shortcut to %SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe -File whatever.ps1. You may need to use the -ExecutionPolicy parameter to allow scripts. Run powershell /? from a Cmd.exe or PowerShell prompt for more information.

Related

How to run powershell script from .ps1 file?

I'm trying to automate the execution of a simple PS script (to delete a certain .txt file). Obviously, I'm new to powershell :)
When I run the code in shell, it works flawless. But when i save the code as a .ps1 and double-click it (or execute it remotely), it just pops up a window and does nothing.
I've tried to save the code as a .bat file and execute it on Windows command line, but it behaves the same: Works by coding directly on prompt, but doesn't Works by executing the .bat file.
$Excel = New-Object -ComObject Excel.Application
$Workbook = $Excel.Workbooks.Open('H:\codes\test1.xlsm')
$workSheet = $Workbook.Sheets.Item(2)
$str_name = $WorkSheet.Cells.Item(2,1).Text
Remove-Item -Path "H:\text files\$str_name.txt" -Force
I expected it to work by double-clicking it, just as it does by running in shell, or in the command line, but i can't figure out why it doesn't.
Create a batch file which points at your .ps1 file. You may be required to run the batch file with elevated permissions, depending on your access levels (the logged in account will be used for execution).
E.g.:
Powershell.exe -executionpolicy remotesigned -File "C:\Path\script.ps1"
If this still isn't working, please execute your batch file via CMD (copying the path, wrapped in quotation marks, into CMD) and let me know the response.
There are several ways to run a .ps1 file. The simplest way is to right-click the file and choose 'Run with PowerShell'.
As others have suggested, you can also run your .ps1 file using powershell.exe either in command prompt or from a BATCH or CMD file. As follows:
powershell.exe -File C:\Script.ps1
If you are still having problems it could be the execution policy. For this, simply add -ExecutionPolicy Bypass to your command as follows:
powershell.exe -File C:\Script.ps1 -ExecutionPolicy Bypass
To change your execution policy you can use:
Set-ExecutionPolicy

Using Start-Process to call a .bat file that calls an .exe, wrong path?

On PowerShell when I issue the command Start-Process c:\Folder\install.bat.
The batch file has this inside:
setup.exe /switch1 /switch2
When I run install.bat on its own, it runs fine. My problem is when I am calling it from PowerShell I notice it is trying to run setup.exe from the path systems32, and it says command not recognized.
How do I run PowerShell and give the correct path to where to run setup.exe? I tried to place a path inside the batch file, unsuccessfully.
I guess setup.exe started with a path will miss some files present in the current folder. So this might be a PoSh way
Set-Location c:\Folder\
Start-Process setup.exe /switch1 /switch2
Or inside the batch change the current directory
Pushd %~dp0
setup.exe /switch1 /switch2
popd
HTH

put a powershell script on path through powershell commands.

I have a powershell script that I want to run from cmd/ps any location by putting it in path.
What is the command that can achieve that ?
I m basically looking for a UNIX equivalent of putting your script in bashrc and thus available from anywhere to run.
echo 'export PATH=$PATH:/path/to/script' >> ~/.bashrc && source ~/.bashrc
In windows you also have the system variable PATH that's used for defining where to locate executables.
You could do the following that should be equivalent assuming you're only using Powershell:
$newPath = "c:\tmp\MyScriptPath";
[Environment]::SetEnvironmentVariable('PATH', "$($env:Path);$newPath", [EnvironmentVariableTarget]::User);
# Update the path variable in your current session; next time it's loaded directly
$env:Path = "$($env:Path);$newPath";
You can then execute your script directly in Powershell with just the name of the script.
However_ : this will not work under cmd because cmd doesn't know how to handle the ps1 script as an executable. Normally one would execute the script from cmd by calling the following:
Powershell.exe -executionpolicy remotesigned -File C:\Tmp\Script.ps1
If this is "unacceptable" for you, the easiest way is to create a bat script along with your ps1 script (same path) and add the following content :
Script.bat (Assuming you have Script.ps1 in the same folder):
#ECHO OFF
PowerShell.exe -Command "& '%~dpn0.ps1'"
PAUSE
This will create the wrapper needed to Invoke Script anywhere in your cmd as batch files can be executed from cmd

PowerShell script to standalone executable

I have a .ps1 file and I wanted to run the script when double-click! But a ps1 file is a script file so when you double click on in it opens a the notepad to edited.
So I what a way to execute the ps1 script!
I know that I can make a bat file and that execute the ps1 file and the problem with that is, that I need to files the .ps1 and a .bat! The problem that I'm facing is that there will be to files and so inconvenient...
So! Is there a way to make exe from a ps1 script?
Import this into the registry:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Classes\Microsoft.PowerShellScript.1\Shell]
#="0"
or run this on the command line:
set "key=HKCU\Software\Classes\Microsoft.PowerShellScript.1\Shell"
reg add "%key%" /v "" /t REG_SZ /d 0 /f
Both operations do the same: change the default action for PowerShell scripts to "Run with PowerShell" for the currently logged in user.
you might try something like http://www.battoexeconverter.com/ You'll still need to create the bat file, but the two files(ps1 and bat) can be combined into a single .exe file for portability.
you might try PS2EXE http://ps2exe.codeplex.com/ (still beta though)

Set up PowerShell Script for Automatic Execution

I have a few lines of PowerShell code that I would like to use as an automated script. The way I would like it to be able to work is to be able to call it using one of the following options:
One command line that opens PowerShell, executes script and closes PowerShell (this would be used for a global build-routine)
A file that I can double-click to run the above (I would use this method when manually testing components of my build process)
I have been going through PowerShell documentation online, and although I can find lots of scripts, I have been unable to find instructions on how to do what I need. Thanks for the help.
From http://blogs.msdn.com/b/jaybaz_ms/archive/2007/04/26/powershell-polyglot.aspx
If you're willing to sully your beautiful PowerShell script with a little CMD, you can use a PowerShell-CMD polyglot trick. Save your PowerShell script as a .CMD file, and put this line at the top:
#PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $('$args=#(^&{$args} %*);'+[String]::Join(';',(Get-Content '%~f0') -notmatch '^^#PowerShell.*EOF$')) & goto :EOF
If you need to support quoted arguments, there's a longer version, which also allows comments. (note the unusual CMD commenting trick of double #).
##:: This prolog allows a PowerShell script to be embedded in a .CMD file.
##:: Any non-PowerShell content must be preceeded by "##"
##setlocal
##set POWERSHELL_BAT_ARGS=%*
##if defined POWERSHELL_BAT_ARGS set POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%
##PowerShell -ExecutionPolicy Bypass -Command Invoke-Expression $('$args=#(^&{$args} %POWERSHELL_BAT_ARGS%);'+[String]::Join(';',$((Get-Content '%~f0') -notmatch '^^##'))) & goto :EOF
Save your script as a .ps1 file and launch it using powershell.exe, like this:
powershell.exe .\foo.ps1
Make sure you specify the full path to the script, and make sure you have set your execution policy level to at least "RemoteSigned" so that unsigned local scripts can be run.
Run Script Automatically From Another Script (e.g. Batch File)
As Matt Hamilton suggested, simply create your PowerShell .ps1 script and call it using:
PowerShell C:\Path\To\YourPowerShellScript.ps1
or if your batch file's working directory is the same directory that the PowerShell script is in, you can use a relative path:
PowerShell .\YourPowerShellScript.ps1
And before this will work you will need to set the PC's Execution Policy, which I show how to do down below.
Run Script Manually Method 1
You can see my blog post for more information, but essentially create your PowerShell .ps1 script file to do what you want, and then create a .cmd batch file in the same directory and use the following for the file's contents:
#ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%MyPowerShellScript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'"
Replacing MyPowerShellScript.ps1 on the 3rd line with the file name of your PowerShell script.
This will allow you to simply double click the batch file to run your PowerShell script, and will avoid you having to change your PowerShell Execution Policy.
My blog post also shows how to run the PowerShell script as an admin if that is something you need to do.
Run Script Manually Method 2
Alternatively, if you don't want to create a batch file for each of your PowerShell scripts, you can change the default PowerShell script behavior from Edit to Run, allowing you to double-click your .ps1 files to run them.
There is an additional registry setting that you will want to modify so that you can run scripts whose file path contains spaces. I show how to do both of these things on this blog post.
With this method however, you will first need to set your execution policy to allow scripts to be ran. You only need to do this once per PC and it can be done by running this line in a PowerShell command prompt.
Start-Process PowerShell -ArgumentList 'Set-ExecutionPolicy RemoteSigned -Force' -Verb RunAs
Set-ExecutionPolicy RemoteSigned -Force is the command that actually changes the execution policy; this sets it to RemoteSigned, so you can change that to something else if you need. Also, this line will automatically run PowerShell as an admin for you, which is required in order to change the execution policy.
Source for Matt's answer.
I can get it to run by double-clicking a file by creating a batch file with the following in it:
C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe LocationOfPS1File
you can use this command :
powershell.exe -argument c:\scriptPath\Script.ps1