I am setting execution policy rights to PowerShell through a batch file to allow running of PowerShell scripts.
Windows PowerShell has execution policy as Restricted by default. Yes we can start PowerShell with elevated (admin) rights and set policy as Unrestricted manually.
Now the question is if we can set it manually then is it makes sense to set it via batch file?
When you need to run your script file on multiple machines (say for testing purpose) it could be a hassle setting policy rights to each machine manually. Although it is not much of a work for a person but this could be an overhead when you ask users to do this setting for themselves. Some users prefer to do as least as possible to make things work.
So the idea is user just need to execute the batch file with admin
rights which will set the policy rights automatically. The path to
execute the script should not be a problem because both the batch file
and PowerShell script would be in same folder (recommended).
This is what i have tried so far in my batch (.bat) file.
powershell.exe -Set ExecutionPolicy Unrestricted -FILE file.ps1 "args[0]" "args[1]"....
Where file.ps1 is the name of PowerShell script and args[0], args[1],…. are the optional arguments passed to the script.
The only thing which is not working here is the execution policy. It could be some syntax issue also. Please advise.
This is the solution which worked for me.
Eventually, the problem was with the syntax as you can notice.