Author Posts

June 23, 2016 at 6:42 pm

Hello,

I am very new to powershell and I am trying to learn and leap into simple things to automate at work. I have a simple batch script that adds values to several registry options. I would like help on automate this batch file with powershell and make it more user interactive. I would like to be able to have user input computer name or username, run the script on any computer on the network. I guess the small the script the better. The batch file is as follow:

reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v DisableLockWorkstation /t REG_DWORD /d 1 /f

reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v DisableLockWorkstation /t REG_DWORD /d 1 /f

reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v StarMenuLogOff /t REG_DWORD /d 1 /f

reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v StarMenuLogOff /t REG_DWORD /d 1 /f

reg add "HKEY_LOCAL_MACHINE\SOFTWARE \Microsoft\Windows\CurrentVersion\Policies\System" /v HideFastUserSwitching /t REG_DWORD /d 1 /f

June 23, 2016 at 7:07 pm

Here are two quick examples... first one prompts the user for the target computer name

# Get user credentials
$cred = Get-Credential

# Prompt for computer name
$computer =  Read-Host -Prompt 'Name of target system'

# Code to execute
$sb = {
    reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v DisableLockWorkstation /t REG_DWORD /d 1 /f

    reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v DisableLockWorkstation /t REG_DWORD /d 1 /f

    reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v StarMenuLogOff /t REG_DWORD /d 1 /f

    reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v StarMenuLogOff /t REG_DWORD /d 1 /f

    reg add "HKEY_LOCAL_MACHINE\SOFTWARE \Microsoft\Windows\CurrentVersion\Policies\System" /v HideFastUserSwitching /t REG_DWORD /d 1 /f
}

# Execute Code
Invoke-Command -ComputerName $computer -Credential $cred -ScriptBlock $sb

The second example is almost identical, but instead of promoting the user for the target computer name you provide it as a parameter on script execution.

Param ([string]$ComputerName)

# Get user credentials
$cred = Get-Credential

# Code to execute
$sb = {
    reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v DisableLockWorkstation /t REG_DWORD /d 1 /f

    reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\System" /v DisableLockWorkstation /t REG_DWORD /d 1 /f

    reg add "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v StarMenuLogOff /t REG_DWORD /d 1 /f

    reg add "HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" /v StarMenuLogOff /t REG_DWORD /d 1 /f

    reg add "HKEY_LOCAL_MACHINE\SOFTWARE \Microsoft\Windows\CurrentVersion\Policies\System" /v HideFastUserSwitching /t REG_DWORD /d 1 /f
}

# Execute Code
Invoke-Command -ComputerName $ComputerName -Credential $cred -ScriptBlock $sb

You would execute this like so...

PS > .\ScriptName.ps1 -ComputerName Server1

Hope that helps.

June 28, 2016 at 8:41 pm

Stefan

Thank you for your help on this, I appreciate it. You rock!!!