Tip : Is Your PowerShell Session Running With Administrative Privileges?

Print Friendly

Sometimes you may need to perform administrative stuff within your script. In such cases, it’s better to check whether your user has such privileges to execute the script.

Please find the sample below, which checks user for administrative privilege;

$user = [Security.Principal.WindowsIdentity]::GetCurrent()
if((New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)){
  # Keep executing your script...
}else{
  # Warn user for required privileges...
  Write-Warning "Please execute the script with administrative privileges."
}

Related posts:

  1. Tip : PowerShell Oturumu Sistem Yöneticisi Olarak Mı Başlatılmış?
  2. PowerShell ile AppFabric Cache Yönetimi
  3. Connecting to TFS with Different User
  4. How to Restrict Internet Explorer Control Panel Settings
  5. AppFabric Cache Management cmdlet
Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!

Leave a Reply

 
QR Code Business Card