gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
How to add if statement to powershell script so it stops if pc name is certain
- User
How to add if statement to powershell script so it stops if pc name is certain
- Assistant
In a PowerShell script, you can use an
ifstatement to check the computer's name and stop the script execution if it matches a certain value. Here’s a basic example of how you might implement this:# Get the current computer name $computerName = $env:COMPUTERNAME # Define the computer name you want to check against $restrictedComputerName = "YourPCName" # Check if the current computer name matches the restricted name if ($computerName -eq $restrictedComputerName) { Write-Host "Script execution stopped: running on restricted computer '$restrictedComputerName'." exit } # Continue with the rest of your script Write-Host "Script is running on an allowed computer."Explanation:
$env:COMPUTERNAMEis used to retrieve the current computer's name.$restrictedComputerNameis the name you're checking against. Replace"YourPCName"with the actual name of the computer on which you want to stop the script.