A Self Elevating Powershell Script

Have you ever wanted to create a self-elevating PowerShell script that runs with administrator privileges without requiring the user to right-click and select “Run as Administrator”? I know I have, and I’ve found a solution that I’m excited to share with you.

Understanding the Need for Self-Elevating Scripts

Before we dive into the technical details, let’s discuss why self-elevating scripts are useful. When working with PowerShell scripts that require administrative privileges, it can be cumbersome to ask users to manually run the script as an administrator. A self-elevating script automates this process, making it more convenient and user-friendly.

Creating a Self-Elevating PowerShell Script

To create a self-elevating PowerShell script, we can utilize a technique called “PowerShell self-elevation”. This involves embedding a manifest file within the script to indicate that it requires administrative privileges.

First, let’s create the manifest file. This XML-based file will specify the requested execution level. Save the following XML code in a file with a .manifest extension:


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>

Next, we need to embed this manifest file into our PowerShell script. We can use the Add-Type cmdlet to achieve this. Here’s an example of how we can do this:


$scriptPath = $MyInvocation.MyCommand.Path
$manifestPath = $scriptPath -replace '\.ps1$', '.manifest'
Add-Type -TypeDefinition @"
using System;
using System.Diagnostics;
using System.Security.Principal;
using System.Runtime.InteropServices;

public class ElevationChecker {
public static bool IsElevated {
get {
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}
"@

Within the script, you can then check if the script is already running with elevated privileges. If not, you can re-run the script with elevated permissions using the Start-Process cmdlet. This ensures that the script runs with the necessary administrative privileges.

Personal Touch: My Experience

When I first implemented a self-elevating PowerShell script, it was a game-changer for me. My users no longer had to go through the hassle of manually running scripts as administrators. The automation provided by self-elevation not only improved user experience but also streamlined our administrative processes.

Conclusion

Creating a self-elevating PowerShell script empowers you to simplify the execution of administrative tasks. By leveraging manifest files and the Add-Type cmdlet, you can elevate the privileges of your scripts seamlessly. I encourage you to explore this technique and experience the convenience it brings to your PowerShell scripting endeavors.