Automate registry entries

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Mario Menendez Mario Menendez 5 months, 1 week ago.

  • Author
    Posts
  • #44570
    Profile photo of Mario Menendez
    Mario Menendez
    Participant

    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

  • #44585
    Profile photo of Stefan Werner
    Stefan Werner
    Participant

    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.

  • #45171
    Profile photo of Mario Menendez
    Mario Menendez
    Participant

    Stefan

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

You must be logged in to reply to this topic.