I am trying to run a PowerShell script Daily.ps1
on start-up, however, due to administrator settings (I cannot run as admin, that is not an option), I cannot run it through the Task Scheduler. For example, this is the contents of Daily.ps1
:
if (1 -eq 1) {
"Hello there!"
}
So I tried to have a batch script
Daily.cmd
run on start up (through the start-up folder), which runs, but I cannot get it run theDaily.ps1
, and I get a message saying running scripts is disabled. (Both files are in the same directory)powershell C:\Users\Simon\Desktop\Daily.ps1
File C:\Users\Simon\Desktop\Daily.ps1 cannot be loaded because running scripts is disabled on this system
I then tried using this line of code from a trick I learned to bypass running scripts directly:
powershell cat Daily.ps1 | powershell invoke-expression
This works but only for one liners. So I added the
-raw
flag forcat
, which works when in powershell, but not in CMD. For some reason,Daily.ps1
's text is still stored as an array of strings. (apologies for formatting)cmdlet Invoke-Expression at command pipeline position 1
Supply values for the following parameters:
Command: if (1 -eq 1) {
- invoke-expression : At line:1 char:14
- if (1 -eq 1) {
- Missing closing '}' in statement block or type definition.
- At line:1 char:1
- invoke-expression ~~~~~~~~~~~~~~~~~
So I tried to add this to
Daily.cmd
:powershell cat -raw Daily.ps1 | powershell-invoke-expression
However, the rest of the script doesn't get executed at all once I enter PowerShell.
I don't know to get Daily.ps1
to run through a batch command. Is there a way I missed, or is one of the ways I tried faulty (without admin rights)?
Edit: To clarify, ExecutionPolicy
is set to Restricted
, and that cannot be changed. Additionally, I can run PowerShell scripts fine through right clicking the file and running with PS.