Skip to the content
Cloudwyse

Cloudwyse

Shaping the future

Menu
  • Home
  • Services
  • Contact Us
  • Blog

Configuring log-on scripts to run via powershell from the “Run” key in Windows registry

Jul 04 2018
0

If you’ve tried to add a powershell script to a registry run key, you’ve probably discovered that it doesn’t run with powershell.  Instead, Windows just opens the script in Notepad and leaves it sitting there all naked and exposed!  So in order to launch the powershell scripts we’re going to need to use a .bat or .cmd file launched from the run key in the registry.  So we’re going to add a string (REG_SZ) to

1
[cc lang="dos"]HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run[/cc]

When you create the key you can call it whatever you like but the value has to be exactly right – and point to the script you want to launch.  In my case this is:

Cloudwyse ScriptsREG_SZcmd /c START /MIN “Cloudwyse Env Execution” cmd /c “C:\Cloudwyse\Scripts\lch_powershell.cmd”

So I’m telling windows to launch a minimised command prompt, and then I’m passing the command I want running within that command prompt which means that users won’t have a big black box pop up on their screen everytime they log in.  Within that .cmd script, I need to launch Powershell but if I just go ahead and use the “powershell -File” command then I will hit issues with the execution policy within Windows which is disabled by default.  So we need the scripts to bypass the Powershell execution policy, but we don’t necessarily want to change the execution policy for the whole server for good.  So we can use this within the .cmd file:

1
[cc lang="dos"]@ECHO OFF SET CloudwyseScriptsDIR=C:\Cloudwyse\Scripts[/cc]
1
[cc lang="dos"]SET PSScriptPath=%CloudwyseScriptsDIR%wrapper.ps1[/cc]
1
[cc lang="dos"]PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PSScriptPath%""' -WindowStyle Hidden}";[/cc]

So we’re asking Windows to launch powershell, temporarily bypassing the execution policy for the context of this command only.  Then we pass the command which also bypasses the execution policy, but this time within the context of the file we’re about to launch which is “wrapper.ps1”.

So now we have our Powershell wrapper script running automatically everytime a user logs in.  We can now nest within that script all the other powershell scripts we would also like to run by doing the following:

1
2
3
4
[cc lang="powershell"]$ScriptPath = Split-Path $MyInvocation.MyCommand.Path
. "$ScriptPathdate_check.ps1"
. "$ScriptPathvmcheck.ps1"
. "$ScriptPathIE_ESC.ps1"[/cc]

Simply add a new line to this script for every logon script you want to run for users.

And if you’re wondering why the “$MyInvocation” variable doesn’t seem to be used anywhere, that’s because it’s an automatic variable.  There’s a great article on it here if you’re interested in learning more.

Posted inautomate cli hklm login script logon microsoft powershell regedit registry run script server 2016 windows windows 10

Post navigation

Previous PostPrevious Powershell script to display message box to user giving them the option to restart now or later
Next PostNext Add AD security group to local administrators on Hyper-V server running core

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent posts

Critical Cisco IOS XR Exploit: Protecting Network Infrastructures

September 26, 2024 0

Generative AI Phishing Scams: How Cybercriminals Are Using AI to Outsmart Users

September 4, 2024 0

Rise of SIM Swapping Attacks: Targeting Cryptocurrency Holders

August 26, 2024 0

Importing certificates from 123-reg.co.uk for use in IIS (applies to many cert providers)

July 4, 2024 0

Search

Follow Us

Follow Us

Facebook-f Twitter Linkedin

Head Office Address

Cloudwyse Ltd
148 Rose Bowl,
Portland Crescent,
Leeds,
LS1 3HB

Contact

UK: 0113 4508608

USA: (832) 843-0608

Email: info@cloudwyse.co.uk

Company Information

Company Number:

11357935

VAT Number:

GB 323 4853 10

Created with Futurio WordPress Theme