To create a PowerShell script that iterates over a list of computer names from a text file and modifies a specific registry key value on each machine, I outlined a script that uses remote registry operations. The script will change the ‘Start‘ value in the USBSTOR key from ‘3‘ to ‘4‘. It’s important to note that this operation requires administrative privileges on the remote machines and that remote registry service should be running.
Here’s how the script could be structured:
# Path to the text file containing the list of computer names $textFilePath = "C:\path\to\your\file.txt" # Read the list of computer names from the text file $computers = Get-Content -Path $textFilePath foreach ($computer in $computers) { try { # Define the registry path and value name $registryPath = "HKLM:\SYSTEM\CurrentControlSet\Services\USBSTOR" $valueName = "Start" $newValue = 4 # Invoke a command on the remote computer to change the registry value Invoke-Command -ComputerName $computer -ScriptBlock { Param($path, $name, $value) Set-ItemProperty -Path $path -Name $name -Value $value } -ArgumentList $registryPath, $valueName, $newValue Write-Host "Registry key updated successfully on $computer" } catch { Write-Host "Failed to update registry key on $computer. Error: $_" } }
In this script:
$textFilePath
should be the path to your text file containing the list of computer names.Get-Content
reads the computer names from the file.- A
foreach
loop iterates over each computer name. Invoke-Command
runs a script block on each remote computer, whereSet-ItemProperty
is used to change the ‘Start‘ value in theUSBSTOR
registry key to ‘4‘.- Error handling with a
try-catch
block is used to manage potential issues, such as connectivity problems or insufficient permissions.
Please ensure:
- The Remote Registry service is running on the remote machines.
- You have appropriate permissions to modify registry settings on the remote machines.
- PowerShell remoting is enabled and properly configured on the remote machines.
VÕ TÌNH THƯƠNG