PowerShell Logon Scripts

PowerShell Logon Scripts

If you are thinking of using PowerShell for a logon script – think again.  Much as I loved grappling with old DOS batch files and perfecting the superior VBScript logon scripts, I admit there are better ways of achieving a task than a PowerShell logon script.  Whatever your worthy project to control the users desktop environment, start with Group Policy.  Incidentally, even if you end up ignoring my advice, you will still probably need a Group Policy to push out your logon script.

Topics For PowerShell Logon Scripts


PowerShell Logon Scripts – If You Must!

Back to PowerShell and logon scripts, PowerShell was designed as a command line method for configuring the operating system and not for tweaking users’ environments.  In particular, the security and script execution methods employed by PowerShell are unsuited to running on client machines.

Even if you succeed in getting a PowerShell logon scripts to run, you have to ask was it worth it?  Surely there must be a better way of providing a default printer or access to a network share.

PowerShell Logon Script Technique

The problem is that you cannot simply assign a .ps1 file as a logon script; because, by design, Windows 7 or Vista won’t be able to execute it.  Fortunately, there is a work-around and that is to create a VBScript script which calls your PowerShell script.  To my mind this whole procedure is telling me clearly, this is not the best way of configuring logon settings.

However, if this is what you want, then PowerShell can play its part via the -ComObject.  Here is a MapNetworkDrive example:

# PowerShell Logon Script Example
$net = $(New-Object -ComObject WScript.Network)
$net.MapNetworkDrive("F:", "\\BigServer\Stuff")

Note 1: Assume that you save the above code in C:\PowerShell\logon.ps1

Note 2: Now call the logon.ps1 file from VBScript thus:

# PowerShell Logon Script Called From VBScript
Set objShell = CreateObject("Wscript.Shell")
objShell.Run("powershell.exe -noexit C:\PowerShell\logon.ps1")

See more on PowerShell MapNetworkDrive

# PowerShell Logon Script Example
$net = $(New-Object -ComObject WScript.Network)
$net.MapNetworkDrive("F:", "\\BigServer\Stuff")

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

PowerShell v 2.0 and Windows 7

Version 2.0 transforms PowerShell’s remoting, Windows 7 and Windows Server 2008 R2 provide the menus to configure PowerShell scripts to run via Group Policy.

Another option for running PowerShell scripts on remote Windows 7 computers is to use logon, logoff, startup, and shutdown scripts defined in GPOs. Earlier versions of Windows supported specifying only command files for these scripts. PowerShell provides far more flexibility and power than is available in command files.  See more on PowerShell in Windows 7

Executing PowerShell Logon Scripts Via Group Policy

When Group Policy was delivered with Windows 2000, it allowed administrators to execute batch file-based scripts at user logon or logoff, as well as at computer startup and shutdown. This enabled administrators to configure parts of the environment or execute additional programs during those times. Windows 7 introduces support for executing PowerShell scripts as well, giving administrators more comprehensive tools for real-time configuration during these system events.  See more on Logon Script Group Policy.

Group Policy, Logon Scripts and PowerShell

Here are the Group Policy paths to set the user and computer logon scripts.

User Configuration\Windows Settings\Scripts (Logon/Logoff).
Computer Configuration\Windows Settings\Scripts (Startup/Shutdown).

PowerShell Logon Scripts

SolarWinds Response Time Viewer for WiresharkGuy Recommends: Response Time Viewer for Wireshark

Here is a free tool to troubleshoot network connection and latency problems.  Key concept: this is a free tool from SolarWinds that analyzes network packets captured by Wireshark (also a free tool).

When you inspect the data in the Response Time Dashboard, if you hover over an application such as Teredo or TCP, then you get an orange box showing a breakdown of network and application response times, note the 'Peak value' in addition to the 'Average'.

Download your free trial of SolarWinds Response Time Viewer for Wireshark

Note: This page has been updated here: Logon Scripts in PowerShell 3

Summary of PowerShell Logon Scripts

Although I spent many a happy hour experimenting with old DOS batch files and perfecting the superior VBScript logon scripts, I now realize there are better ways of achieving a task than a PowerShell logon script, start with Group Policy.

If you like this page then please share it with your friends


See more Microsoft PowerShell tasks:

PowerShell Home   • Shell Application   • New-Object   • PowerShell Add Printer   • PowerShell -com

PowerShell Logon Script  • Map Network Drive  • PowerShell Create Shortcut  • Free CSV Import Tool

Invoke-Expression   • Invoke-Command   • Invoke-Item   • PowerShell Expression v Command Mode

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.