There is a PowerShell script that runs on ps startup, if it exists. The filespec for this script is in the variable $profile
.
You can use PowerShell commands to check to see if this script file exists, to create it if it doesn't, and to edit it with notepad. Here's the how-to guide.
Note that in recent (~2020 and later) versions, PowerShell will no longer run unsigned scripts (not even $profile
!) by default. If you just follow the old instructions such as in that how-to guide, when you open a new PowerShell, you'll see an error message like:
. : File C:\[..]\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 cannot be loaded
because running scripts is disabled on this system. For more information,
see about_Execution_Policies at
https:/go.microsoft.com/fwlink/?LinkID=135170.
Signing your profile once — let alone every time you change it — is probably not realistic, so you must change the Execution Policy to allow it.
To do this, you can either:
Run the following command as Administrator:
Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
or, use RegEdit to modify Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell
, adding a new REG_SZ with the name ExecutionPolicy
and the value RemoteSigned
.
Both have exactly the same effect and will persist across sessions.
The RemoteSigned
policy requires that scripts downloaded from elsewhere be signed, but scripts you create locally on your machine (such as $profile
) can run without a signature. (You could also set the policy to Unrestricted
to enable unsigned downloaded scripts as well, but this isn't recommended security practice.)
Best Answer
In Task Scheduler, put the admin credentials in there under Security Options on the General tab (select "Run whether user is logged on or not", you will be prompted for password when you save).
Alternatively, if you know the user is going to be an admin, you could probably just tick "Run with highest privileges" but you'll want to test that.
If you're using schtasks there are options to provide username and password (just note these must be provided in plain text) for this purpose and I believe in later versions of PowerShell there are means of managing scheduled tasks but I am not familiar with them. EDIT: PS scheduled tasks run as PS Sessions which run straight into the double-hop authentication issue when trying to remote so schtasks it is...