Link to home
Start Free TrialLog in
Avatar of Murty M
Murty MFlag for India

asked on

How to run PowerShell script ps1 file from the command line as an administrator.

How to run PowerShell script ps1 file from the command line as an administrator.

Example: 

I have  Carlwebster script XD7_Inventory_V2_Signed.ps1, Downloaded from the below URL.

https://carlwebster.com/downloads/download-info/xenappxendesktop-7-8/

How can I run this PS1 file from the command line as an administrator?

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

if you need to pass arguments to the script

# Get the ID and security principal of the current user account
$myWindowsID = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$myWindowsPrincipal = New-Object System.Security.Principal.WindowsPrincipal($myWindowsID);

# Get the security principal for the administrator role
$adminRole = [System.Security.Principal.WindowsBuiltInRole]::Administrator;

# Check to see if we are currently running as an administrator
if ($myWindowsPrincipal.IsInRole($adminRole))
{
    # We are running as an administrator, so change the title and background colour to indicate this
    $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)";
    $Host.UI.RawUI.BackgroundColor = "DarkBlue";
    Clear-Host;
}
else {
    # We are not running as an administrator, so relaunch as administrator

    # Create a new process object that starts PowerShell
    $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";

    # Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path
    $newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"

    # Indicate that the process should be elevated
    $newProcess.Verb = "runas";

    # Start the new process
    [System.Diagnostics.Process]::Start($newProcess);

    # Exit from the current, unelevated, process
    Exit;
}

# Run your code that needs to be elevated here...

Write-Host -NoNewLine "Press any key to continue...";
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown");

Open in new window


if not then this will relaunch the script as an admin add this to the top of the script
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Start-Process powershell.exe "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs; exit }

# Your script here 

Open in new window

Avatar of Murty M

ASKER

https://www.javatpoint.com/powershell-run-as-administrator
Step 1: Open the Command Prompt, and type the PowerShell as a command, then press Enter key.
Step 2: Now, the command prompt will turn to Windows PowerShell.
Step 3: Type the command start-process PowerShell -verb runas and press the "enter" key.
Step 4: It will bring up an elevated Windows PowerShell as an administrator.
https://theitbros.com/run-powershell-script-as-administrator/
Start-Process powershell -verb runas -ArgumentList "-file C:\PS\Scripts\myPSScript.ps1"

I need a command line (single or multiple cmd lines) to run a Script file from Windows Command-Line.
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial