1

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!"
}  
  1. 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 the Daily.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

  2. 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 for cat, 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 ~~~~~~~~~~~~~~~~~
  3. 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.

Simon
  • 39
  • 1
  • 9
  • Additionally, I cannot download any software. If I can do this strictly through batch or PowerShell, that would be preferable – Simon Jul 18 '19 at 20:50
  • It may just be that your execution policy has not been set from the default `Restricted`. Try to invoke powershell.exe with command line options, possibly like this: `PowerShell -File "C:\Users\sixu\Desktop\Daily.ps1" -NoProfile -NoLogo -ExecutionPolicy RemoteSigned` – Compo Jul 18 '19 at 21:08
  • I cannot run the script regardless since the `ExecutionPolicy` is set to `Restricted` and I cannot change that. – Simon Jul 18 '19 at 21:09
  • there are apparently ways to _totally block_ running PoSh. i think `applocker` is one such. you likely need to talk to your net/sys admin about this. – Lee_Dailey Jul 18 '19 at 21:10
  • Did you use the command line I have provided? or is that simply a reactive response? We cannot know if ideas work if you do not try them! – Compo Jul 18 '19 at 21:10
  • Yes, I did try it. I just added an edit to clarify. – Simon Jul 18 '19 at 21:13
  • 1
    Perhaps you could ask somebody else to say hello to you every day instead :-) – Compo Jul 18 '19 at 21:35
  • 1
    This could be helpful: [How do I run a PowerShell script when the computer starts?](https://stackoverflow.com/questions/20575257/how-do-i-run-a-powershell-script-when-the-computer-starts) or this: [How can a PowerShell script be automatically run on startup?](https://superuser.com/questions/688007/how-can-a-powershell-script-be-automatically-run-on-startup) – techguy1029 Jul 18 '19 at 22:06
  • I tried both those options, however, the problem is that I cannot change the `ExecutionProperty` to anything other than `restricted`. Which seems to be the root of this issue. Given my circumstances, that property must stay `restricted`. But for some reason, I am still able to run `.ps1` files through right clicking and "run with powershell". I expected there to be a way to execute any GUI actions through the command line, but maybe that is not the case for PowerShell Scripts? – Simon Jul 19 '19 at 00:39

2 Answers2

0

Create a scheduled task to run at computer startup. Put powershell.exe in the field "program/script" and -File "C:\path\to\your.ps1" in the field "arguments" (you may want to avoid placing the script in a user profile). Set the task to run whether the user is logged on or not.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • I hate repeating myself, but all these methods do not work be cause they assume I can run PS scripts through the command line normally. All these methods require `ExecutionProperty` to be set to something other than `restricted`, which cannot happem, the property must stay `restricted`. However, I can run the PS script normally through the windows interface, by rightclicking the file and running with PS. Thus I assumed there must be a way to execute any UI commands through the command line. Perhaps that is a poor assumption, but one would think the command line were more powerful. – Simon Jul 19 '19 at 12:47
  • @Simon Then change the execution policy to something sensible like "RemoteSigned", or add `-ExecutionPolicy Bypass` to the arguments. If you can do neither then the execution policy is most likely administratively set through a group policy, in which case you should talk to your IT department. Otherwise you might be violating company policies. – Ansgar Wiechers Jul 19 '19 at 13:37
-1

I found an answer!

After trying many different methods, I came across this line of code that allows you to run PS scripts if ExecutionProperty is set to restricted:

start powershell "cat -raw C:\Users\Simon\Desktop\Daily.ps1 | invoke-expression"

This runs powershell and uses the trick of piping the results of cat -raw [file.ps1] to invoke-expression. This is useful workaround if ExecutionProperty is set to restricted.

Then you can save this line to a .cmd or .bat file and use either Task Scheduler (more customizability) or put it in the startup folder.

P.S. for everyone who kept saying change the ExecutionProperty to something other than restricted. I clearly stated multiple times that I cannot do that(not admin), nor will the Sys Admin do that, nor will it ever happen(must stay restricted) :)

Simon
  • 39
  • 1
  • 9