Skip to content
TheChatArchiveThe Chat Archive
gpt-4o-2024-08-06· OpenAIAi2 WildChat-4.8M (AI2)

In a PowerShell script, you can use an if statement to check the computer’s name and…

  1. User

    In a PowerShell script, you can use an if statement to check the computer’s name and stop the script execution if it matches a certain value. Do not ise special indentation and symbols

  2. Assistant

    In a PowerShell script, you can use an if statement to check the computer's name and stop the script execution if it matches a certain value by using the Exit command to terminate the script. Below is a simple example of how you might do this:

    # Get the computer's name
    $computerName = $env:COMPUTERNAME
    
    # Specify the name to check against
    $blockedName = "BlockedPC"
    
    # Check if the computer's name matches the blocked name
    if ($computerName -eq $blockedName)
    {
        Write-Host "Script execution stopped. Computer name is blocked: $computerName"
        Exit
    }
    
    # Continue with the rest of the script
    Write-Host "Computer name is allowed: $computerName"
    

    In this script:

    • $env:COMPUTERNAME retrieves the current computer's name.
    • $blockedName is the name you want to block.
    • The if statement checks if the current computer name is blocked.
    • If it matches, a message is displayed and Exit stops the script execution.
    • If not, the script continues with a message indicating that the computer name is allowed.