By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
World of SoftwareWorld of SoftwareWorld of Software
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Search
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
Reading: How to Temporarily Grant Admin Privileges with PowerShell (Securely and Automatically) | HackerNoon
Share
Sign In
Notification Show More
Font ResizerAa
World of SoftwareWorld of Software
Font ResizerAa
  • Software
  • Mobile
  • Computing
  • Gadget
  • Gaming
  • Videos
Search
  • News
  • Software
  • Mobile
  • Computing
  • Gaming
  • Videos
  • More
    • Gadget
    • Web Stories
    • Trending
    • Press Release
Have an existing account? Sign In
Follow US
  • Privacy
  • Terms
  • Advertise
  • Contact
Copyright © All Rights Reserved. World of Software.
World of Software > Computing > How to Temporarily Grant Admin Privileges with PowerShell (Securely and Automatically) | HackerNoon
Computing

How to Temporarily Grant Admin Privileges with PowerShell (Securely and Automatically) | HackerNoon

News Room
Last updated: 2025/04/03 at 2:52 AM
News Room Published 3 April 2025
Share
SHARE

Managing administrative privileges securely is crucial in any IT environment. One common scenario involves temporarily granting administrative rights to a user, often for specific tasks, then revoking them automatically to reduce security risks. To simplify this process, I’ve crafted a powerful PowerShell script that streamlines this entire workflow.

What Does This Script Do?

This PowerShell script safely grants a user temporary admin privileges on a remote Windows machine. It automates privilege escalation and schedules automatic revocation after a user-defined duration.

Core Features and Functionality

  1. Administrator Privileges Check

    The script starts by verifying it’s running with administrative rights. If not, it automatically re-launches itself with the necessary privileges.

  2. User-Friendly Inputs

    It prompts clearly for essential inputs:

    • Hostname or IP of the remote system.

    • Username to grant temporary admin privileges.

    • Duration (in hours or fractions thereof, like 0.5 for half an hour).

  3. Automated WinRM Management

    The script intelligently checks if Windows Remote Management (WinRM) is running on the target machine. If it’s off, the script starts it temporarily and ensures it stops afterward to maintain security:

  4. Temporary Admin Assignment

    The selected user is automatically added to the local administrators group on the remote system

  5. Automatic Privilege Revocation

    The script automatically creates a cleanup script (RevokeAdmin.ps1) on the remote system to revoke admin privileges

Why use this script?

• Enhanced Security: Reduces risks associated with permanent admin privileges.

• Automated and Foolproof: Minimizes human error by automating the entire grant-and-revoke process.

• Flexible: Supports fractional hours for precision privilege management.

• Auditability: Each step provides clear, color-coded logging for easy tracking and auditing.

How to Execute

Simply run the script with administrative privileges, and follow the intuitive prompts.

Use Case Scenarios

• Temporary elevated permissions for helpdesk tasks.

• Short-term admin rights for software installations.

• Troubleshooting administrative issues remotely.

Wrapping Up

Implementing this automated solution enhances your organization’s operational efficiency and security posture significantly. By granting administrative privileges only when absolutely necessary—and revoking them automatically—you minimize potential security breaches and enforce better compliance standards.

Remember, “Now you know, and knowing is half the battle!”

#Another        /_[]_/
#    fine      |] _||_ [|
#       ___     / || /
#      /___       ||
#     (|0 0|)      ||
#   __/{U/}_ ___/vvv
#  /   {~}   / _|_P|
#  | /  ~   /_/   []
#  |_| (____)        
#  _]/______  Barberion  
#     __||_/_     Production      
#    (_,_||_,_)
#
# Run with admin privileges
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Start-Process PowerShell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$($myInvocation.MyCommand.Definition)`"" -Verb RunAs
    exit
}

# Define cleanup function to disable WinRM if it was started by the script
function Cleanup {
    if ($winrmStartedByScript -eq $true) {
        Write-Host "WinRM was started by this script. Disabling WinRM on $remoteSystem..." -ForegroundColor Cyan
        $stopResult = $service.InvokeMethod('StopService', $null)
        Start-Sleep -Seconds 2

        $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" -ComputerName $remoteSystem
        if ($service.Started -eq $false) {
            Write-Host "WinRM service on $remoteSystem stopped successfully." -ForegroundColor Green
        } else {
            Write-Host "Failed to stop WinRM service on $remoteSystem." -ForegroundColor Red
        }
    }
}

# Use a try block to ensure cleanup always ocurs
try {
    # Prompt for rem sys and user
    $remoteSystem = Read-Host "Enter the hostname or IP of the remote system"
    $username = Read-Host "Enter the username to grant temporary admin access"
    
    # Prompt for duration in hrs
    do {
        $durationHours = Read-Host "Enter the duration in hours (e.g., 1 for 1 hour, 0.5 for half hour)"
        if (-not [double]::TryParse($durationHours, [ref]$null)) {
            Write-Host "Invalid input. Please enter a valid numeric value for the duration." -ForegroundColor Red
        }
    } while (-not [double]::TryParse($durationHours, [ref]$null))

# Convert duration from hrs to mins
$durationMinutes = [math]::Round([double]$durationHours * 60)


    # Check WinRM status and start necesary
    $serviceName = "winrm"
    Write-Host "Checking WinRM status on $remoteSystem..." -ForegroundColor Cyan
    $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" -ComputerName $remoteSystem

    if ($service.Started -eq $false) {
        Write-Host "WinRM is not running. Starting WinRM service on $remoteSystem..." -ForegroundColor Cyan
        $startResult = $service.InvokeMethod('StartService', $null)
        Start-Sleep -Seconds 2

        $service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'" -ComputerName $remoteSystem

        if ($service.Started -eq $true) {
            Write-Host "WinRM service on $remoteSystem started successfully." -ForegroundColor Green
            $winrmStartedByScript = $true
        } else {
            Write-Host "Failed to start WinRM service on $remoteSystem. Return code: $($startResult.ReturnValue)" -ForegroundColor Red
            exit
        }
    } else {
        Write-Host "WinRM is already running on $remoteSystem." -ForegroundColor Green
    }

    # Add user to Admin grp
    Write-Host "Adding $username to Administrators group on $remoteSystem..." -ForegroundColor Cyan
    Invoke-Command -ComputerName $remoteSystem -ScriptBlock {
        param ($user)
        Add-LocalGroupMember -Group "Administrators" -Member $user
        Write-Host "User $user added to Administrators group."
    } -ArgumentList $username -ErrorAction Stop

    # Create the RevokeAdmin.ps1 file in C:Temp on the remsys
    Write-Host "Creating RevokeAdmin.ps1 on $remoteSystem in C:Temp..." -ForegroundColor Cyan
    Invoke-Command -ComputerName $remoteSystem -ScriptBlock {
        param ($user)
        $revokeScriptPath = "C:TempRevokeAdmin.ps1"
        $revokeScriptContent = @"
$user = '$user'
Remove-LocalGroupMember -Group "Administrators" -Member $user
"@
        if (-not (Test-Path -Path (Split-Path -Path $revokeScriptPath))) {
            New-Item -ItemType Directory -Path (Split-Path -Path $revokeScriptPath) -Force | Out-Null
        }
        Set-Content -Path $revokeScriptPath -Value $revokeScriptContent
        Write-Host "RevokeAdmin.ps1 created successfully."
    } -ArgumentList $username -ErrorAction Stop

# Schedule revocation
Write-Host "Scheduling revocation of admin privileges in $durationMinutes minutes..." -ForegroundColor Cyan
Invoke-Command -ComputerName $remoteSystem -ScriptBlock {
    param ($duration)
    $revokeScriptPath = "C:TempRevokeAdmin.ps1"
    $taskName = "RevokeAdminAccess"

    # Check if the scheduled task exists and delete it if necessary
    if (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) {
        Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
        Write-Host "Existing scheduled task '$taskName' found and deleted." -ForegroundColor Yellow
    }

    # Schedule the new task
    $time = (Get-Date).AddMinutes($duration).ToString("yyyy-MM-ddTHH:mm:ss")
    $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -NoProfile -File `"$revokeScriptPath`""
    $trigger = New-ScheduledTaskTrigger -Once -At $time
    $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest
    Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName $taskName
    Write-Host "Revocation task scheduled successfully." -ForegroundColor Green
} -ArgumentList $durationMinutes -ErrorAction Stop


    Write-Host "Admin privileges granted for $durationMinutes minutes on $remoteSystem." -ForegroundColor Green
} finally {
    # Cleanup to disable WinRM if it was started by the script
    Cleanup
}

# Remember kids: "Now you know, and knowing is half the battle"
Write-Host "Process completed successfully." -ForegroundColor Green

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Email Print
Share
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Nintendo Switch 2 specs unveiled: Bigger screen, better performance, and more
Next Article Found: British Military Documents, Scattered on the Street
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Stay Connected

248.1k Like
69.1k Follow
134k Pin
54.3k Follow

Latest News

Save over $300 on a robot vacuum that’s probably smarter than you
News
Free Script Writing Templates for Professional Screenwriting
Computing
Apple’s latest MacBook Air just fell to $837 for Mother’s Day
News
3 underrated CarPlay features everyone should be using
News

You Might also Like

Computing

Free Script Writing Templates for Professional Screenwriting

23 Min Read
Computing

Top 10 Clockify Alternatives For Time Tracking & Productivity

26 Min Read
Computing

TikTok faces large-scale content removal after major falling out with Universal Music Group · TechNode

3 Min Read
Computing

Ad Hoc Meeting Essentials: 7 Key Steps for Success |

28 Min Read
//

World of Software is your one-stop website for the latest tech news and updates, follow us now to get the news that matters to you.

Quick Link

  • Privacy Policy
  • Terms of use
  • Advertise
  • Contact

Topics

  • Computing
  • Software
  • Press Release
  • Trending

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

World of SoftwareWorld of Software
Follow US
Copyright © All Rights Reserved. World of Software.
Welcome Back!

Sign in to your account

Lost your password?