Executing a powershell script through batch file
Solution 1
You can still run the script - you just need to tell PowerShell to bypass the system's executionpolicy like this:
powershell.exe -executionpolicy bypass -file \\server\share\yourscript.ps1
Solution 2
Posted it also here:
https://stackoverflow.com/questions/46070152/how-to-run-powershell-command-in-batch-file/65911978#65911978
Following this thread:
https://community.idera.com/database-tools/powershell/powertips/b/tips/posts/converting-powershell-to-batch
you can convert any PowerShell script into a batch file easily using this PowerShell function:
function Convert-PowerShellToBatch
{
param
(
[Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)]
[string]
[Alias("FullName")]
$Path
)
process
{
$encoded = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes((Get-Content -Path $Path -Raw -Encoding UTF8)))
$newPath = [Io.Path]::ChangeExtension($Path, ".bat")
"@echo off`npowershell.exe -NoExit -encodedCommand $encoded" | Set-Content -Path $newPath -Encoding Ascii
}
}
To convert all PowerShell scripts inside a directory, simply run the following command:
Get-ChildItem -Path <DIR-PATH> -Filter *.ps1 |
Convert-PowerShellToBatch
Where is the path to the desired folder. For instance:
Get-ChildItem -Path "C:\path\to\powershell\scripts" -Filter *.ps1 |
Convert-PowerShellToBatch
To convert a single PowerShell script, simply run this:
Get-ChildItem -Path <FILE-PATH> |
Convert-PowerShellToBatch
Where is the path to the desired file.
The converted files are located in the source directory. i.e., <FILE-PATH> or <DIR-PATH>.
Related videos on Youtube
Csothcott
Updated on September 18, 2022Comments
-
Csothcott over 1 year
I am trying to execute a powershell script through a batch file, at the moment I am only testing this to get it running so the .ps1 file is simply a hello world script.
This ALMOST works fine except there is a spanner to throw into the works;
I am trying to have the .ps1 stored in a remote location (on a shared NAS to be specific). This works when I have the .ps1 saved locally and point the .bat to the local .ps1.
However now that the .ps1 is in the remote location I am thrown with the following error;
"file (pathname) cannot be loaded. The file (pathname) is not digitally signed. The script will not execute on the system"
Now I am aware of execution policies and mine are as follows;
MachinePolicy = RemoteSigned UserPolicy = Undefined Process = Undefined CurrentUser = RemoteSigned LocalMachine = Bypass
I have tried changing CurrentUser to Bypass but I am thrown with the following;
Windows Powershell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope
Any ideas would be greatly appreciated!
Sorry if this has been a lot of information! If any of it is unclear feel free to ask me to clarify! I'm fairly new to all of this!
-
alMubarmij over 2 years@Mistercode: superuser.com/a/1677786/1430948 How to use this batch file with .ps1 scripts?
-
-
Csothcott about 8 yearsI think you might be about to make me look very silly.... I tested this on my PC first, which is domain connected and will have access to the NAS. The PC i have been second testing this on is just a local account! Never even crossed my mind! I'll give it a try! Thanks! :)
-
Csothcott about 8 yearsUnfortunately no Joy, there is no restrictions on the NAS anyway and I can access it without entering any credentials on the non domain PC :(
-
Csothcott about 8 yearsUnfortunately I can not use this as a work around. We are using an external program to send packages to the PC. This package can send .bat but not .ps1, this is the reason for having to have .bat files launch the .ps1 if this makes sense?
-
LPChip about 8 yearsWhy not? Your .bat file accesses the .ps1 file in a way doesn't it? So if your .bat file accesses the .ps1 file, it can also copy it. If you edit your original post and include the contents of your .bat file (feel free to rename some paths) I'll edit my answer and write you how the .bat file should look.