Sometime it is nice to be able to elevate a script to admin level without manually doing so.
The script below tries to elevate itself to be run as administrator and the user running it must be a member of the Administrator role on the machine running the script.
# Check and run the script as administrator if possibleparam([switch]$Elevated)functionTest-Admin{$currentUser=New-ObjectSecurity.Principal.WindowsPrincipal$([Security.Principal.WindowsIdentity]::GetCurrent())$currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)}if((Test-Admin)-eq$false){if($elevated){# tried to elevate, did not work, abortingWrite-Output"Cannot run with elevated rights, exiting..."}else{Start-Processpowershell.exe-VerbRunAs-ArgumentList('-noprofile -file "{0}" -elevated'-f($myinvocation.MyCommand.Definition))}exit}# This line will only be executed if elevation is successful, and it's here the rest of your script starts...Write-Output"Successfully elevated script and is now running with full privileges..."