2

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.

9
  • Additionally, I cannot download any software. If I can do this strictly through batch or PowerShell, that would be preferable Commented Jul 18, 2019 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 Commented Jul 18, 2019 at 21:08
  • I cannot run the script regardless since the ExecutionPolicy is set to Restricted and I cannot change that. Commented Jul 18, 2019 at 21:09
  • 1
    Perhaps you could ask somebody else to say hello to you every day instead :-) Commented Jul 18, 2019 at 21:35
  • 1
    This could be helpful: How do I run a PowerShell script when the computer starts? or this: How can a PowerShell script be automatically run on startup? Commented Jul 18, 2019 at 22:06

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 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.
-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) :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.