# Powershell Ruan As Administrator

**URL:** https://forums.powershell.org/t/powershell-ruan-as-administrator/2790
**Category:** PowerShell Help
**Created:** [July 7, 2014, 5:40am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790 "2014-07-07T05:40:34Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mr-jacko](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mr-jacko](https://forums.powershell.org/u/mr-jacko)
#### Post date: [July 7, 2014, 5:40am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/1 "2014-07-07T05:40:34Z")

</div>

Sorry for my bad English.  
I wanted to ask if there is a solution for this problem.  
Run powershell in administrator mode and then run a command that writes to me in a file the characteristics of the host it runs on.  
By executing these commands:  
Start-Process-Verb RunAs PowerShell  
$ computers = Get-WmiObject-Class Win32\_ComputerSystem  
$ computer | out-file c: \ filename.txt  
the thing works.  
I tried to put these lines in a file .ps1 and run  
What happens is that I open powershell, run the first line (Start-Process PowerShell-Verb RunAs), which in turn opens a new shell in administrator mode but then the execution of subsequent commands continue in the original shell (the one without administrator rights) and it generates error.  
In practice, I wanted to ask if there is a way to pass the commands in the .ps1 from the shell normal in to with administrative rights.  
Thank you

---

<div class="post-metadata">

### Author: ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)
#### Post date: [July 7, 2014, 5:43am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/2 "2014-07-07T05:43:47Z")

</div>

Look at the -Command parameter of PowerShell.exe. That’s how you launch a new copy of PowerShell and pass in commands.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [July 7, 2014, 5:52am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/3 "2014-07-07T05:52:15Z")

</div>

Here are a couple of examples on how you could accomplish this:

> **[PowerShell Script that Relaunches as Admin](https://rkeithhill.wordpress.com/2013/04/05/powershell-script-that-relaunches-as-admin/)**
>
> If were following good security practices we run our Windows system with UAC enabled.  This means that if you forget to launch your PowerShell prompt as Administrator when you run a script tha…

[http://blogs.msdn.com/b/virtual\_pc\_guy/archive/2010/09/23/a-self-elevating-powershell-script.aspx](http://blogs.msdn.com/b/virtual_pc_guy/archive/2010/09/23/a-self-elevating-powershell-script.aspx)

---

<div class="post-metadata">

### Author: ![mr-jacko](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mr-jacko](https://forums.powershell.org/u/mr-jacko)
#### Post date: [July 7, 2014, 6:02am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/4 "2014-07-07T06:02:29Z")

</div>

Thank you.  
I can create a script without operator intervention?  
I did not understand, however, how then to pass the following commands:  
$ Computers = Get-WmiObject-Class Win32\_ComputerSystem  
$ Computer | out-file c: \ filename.txt  
Sorry for the banality of the questions but I’m a newbie …

---

<div class="post-metadata">

### Author: ![alexander-johansson](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@alexander-johansson](https://forums.powershell.org/u/alexander-johansson)
#### Post date: [July 8, 2014, 8:29pm UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/5 "2014-07-08T20:29:50Z")

</div>

Hello!

The following should do what you want if I understand your question correctly:

```
$args = @'
powershell.exe -Command {
    $computers = Get-WmiObject -Class Win32_ComputerSystem
    $computers | out-file "c:\temp\filename.txt"
}
'@

Start-Process -ArgumentList $args -Verb RunAs PowerShell
```

---

<div class="post-metadata">

### Author: ![mr-jacko](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mr-jacko](https://forums.powershell.org/u/mr-jacko)
#### Post date: [July 13, 2014, 2:35am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/6 "2014-07-13T02:35:22Z")

</div>

@Alexander Johansson  
I’m sorry, I’m testing your solution but finding a security issue:  
Could not load file C: \ Users \ test \ Desktop \ New Folder \ prova1.ps1. The execution of scripts is disabled on your system. For more information,  
see about\_Execution\_Policies at [about Execution Policies - PowerShell | Microsoft Docs](http://go.microsoft.com/fwlink/?LinkID=135170).  
+ CategoryInfo: Security error: (🙂 , ParentContainsErrorRecordException  
+ FullyQualifiedErrorId: unauthorizedaccess

I tried to use the -ExecutionPolicy Unrestricted but I always get the same error:

$args = @’  
powershell.exe -ExecutionPolicy Unrestricted -Command {  
$computers = Get-WmiObject -Class Win32\_ComputerSystem  
$computers | out-file “c:\youfilename.txt”  
$EmailFrom = “[notifications@somedomain.com](mailto:notifications@somedomain.com)”  
$EmailTo = “[mail@gmail.com](mailto:mail@gmail.com)”  
$Subject = “Notification from XYZ”  
$Body = “this is a notification from XYZ Notifications…”  
$SMTPServer = “[smtp.gmail.com](http://smtp.gmail.com)”  
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)  
$SMTPClient.EnableSsl = $true  
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“login”, “password”);  
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)  
$emailMessage = New-Object System.Net.Mail.MailMessage  
$emailMessage.From = $EmailFrom  
$emailMessage.To.Add($EmailTo)  
$emailMessage.Subject = $Subject  
$emailMessage.Body = $Body  
$emailMessage.Attachments.Add(“C:\youfilename.txt”)  
$SMTPClient.Send($emailMessage)  
}  
'@

Start-Process -ArgumentList $args -Verb RunAs PowerShell

---

<div class="post-metadata">

### Author: ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)
#### Post date: [July 13, 2014, 10:44am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/7 "2014-07-13T10:44:09Z")

</div>

This line should give you what you need:  
Get-WmiObject -Class Win32\_ComputerSystem | select \* | Out-File ($env:COMPUTERNAME + “.txt”)

To make this runs as administrator, on this or any machine, whether they’re on the same domain or not, you can use something like:

```
$DomainAccount = "domain\user-with-eoungh-permissions-for-task"
    $Targets = @("PC1","PC2","PC3") # or get Target computer list from AD, or network scan, or...
    #
    if (!(Test-Path -Path ".\DomainCred.txt")) {
        Write-Output "Error: missing encrypted pwd file .\DomainCred.txt, enter the pwd to be encrypted and saved to .\DomainCred.txt for future script use:" 
        Read-Host 'Enter the pwd to be encrypted and saved to .\DomainCred.txt for future script use:' -AsSecureString | ConvertFrom-SecureString | Out-File .\DomainCred.txt
    }
    $Pwd = Get-Content .\DomainCred.txt | ConvertTo-SecureString
    $DomainCred = New-Object System.Management.Automation.PSCredential($DomainAccount,$Pwd)
    foreach ($Computer in $Targets) {
        $PC = Invoke-Command -ComputerName $Computer -Credential $DomainCred -ScriptBlock { 
            $data = Get-WmiObject -Class Win32_ComputerSystem | select *
            return $data
        } 
        $PC | Out-File ($Computer + ".txt")
    }
```

---

<div class="post-metadata">

### Author: ![mr-jacko](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mr-jacko](https://forums.powershell.org/u/mr-jacko)
#### Post date: [July 13, 2014, 11:03am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/8 "2014-07-13T11:03:00Z")

</div>

Thanks!  
your script starts to be too complicated for my skills (beginning now …).  
what is proposed @Alexander Johansson and built by me by sending email, works if only I could overcome the problem of “The execution of scripts is disabled on your system” without tripping the operator with manual controls as “run as administrator”  
would not be possible to modify the script, as follows so as to overcome the problem?

---

<div class="post-metadata">

### Author: ![alexander-johansson](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@alexander-johansson](https://forums.powershell.org/u/alexander-johansson)
#### Post date: [July 14, 2014, 5:11am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/9 "2014-07-14T05:11:17Z")

</div>

Hello!

I prefer to answer here so that everyone can see the answers 🙂

Try to open the powershell console and use the “Set-ExecutionPolicy” cmdlet inside of the console instead of inside the script file, like this:

```
Set-ExecutionPolicy 'Unrestricted'
```

You can’t change the execution policy inside of a script, you need to do it from the console or by the help of a GPO.

Best Regards Alexander

---

<div class="post-metadata">

### Author: ![mr-jacko](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mr-jacko](https://forums.powershell.org/u/mr-jacko)
#### Post date: [July 14, 2014, 5:19am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/10 "2014-07-14T05:19:25Z")

</div>

So it seems to work, but my goal is to put everything in a script, complementing what I had suggested in your first post .  
Execution requires a response (Y / N).  
I tried ECHO Y | Set-ExecutionPolicy ‘Unrestricted’ but does not work …  
Thank you for the swift and accurate response

---

<div class="post-metadata">

### Author: ![alexander-johansson](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@alexander-johansson](https://forums.powershell.org/u/alexander-johansson)
#### Post date: [July 14, 2014, 5:47am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/11 "2014-07-14T05:47:22Z")

</div>

Hi again!

If you change the execution policy from the console the setting will be permanent.

/Alexander

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [July 14, 2014, 5:57am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/12 "2014-07-14T05:57:15Z")

</div>

If you want Set-ExecutionPolicy to work without prompting, add the -Force switch as well. (However, as has been noted, you can’t rely on doing this from inside a script, since the execution policy would already have to be set up to allow scripts to run before that line was executed anyway.)

piping (echo “Y”) to a command is an old Command Prompt trick which sometimes worked, but is never appropriate when working with PowerShell cmdlets or functions.

---

<div class="post-metadata">

### Author: ![alexander-johansson](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@alexander-johansson](https://forums.powershell.org/u/alexander-johansson)
#### Post date: [July 14, 2014, 6:07am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/13 "2014-07-14T06:07:50Z")

</div>

Yes I’m sorry that I wasn’t so straight forward in my answer, you can change the execution policy from within a script but not if the execution policy is set to restricted.

/Alexander

---

<div class="post-metadata">

### Author: ![mr-jacko](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mr-jacko](https://forums.powershell.org/u/mr-jacko)
#### Post date: [July 14, 2014, 6:16am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/14 "2014-07-14T06:16:50Z")

</div>

unfortunately the problem is that I should perform the execution of the policy manually on many PCs, located in different places.  
for this I tried to automate the process …  
also to perform Set-ExecutionPolicy ‘Unrestricted’ -Force I need to give starting powershell console as administrator on each location …  
sorry for my questions!

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)
#### Post date: [July 14, 2014, 6:20am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/15 "2014-07-14T06:20:47Z")

</div>

If you’re in an Active Directory environment, you can use Group Policy to change PowerShell’s execution policy. Also, individual users can set it for themselves without admin rights; you just need to add the “-Scope CurrentUser” parameter to the Set-ExecutionPolicy call. For example:

```
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force
```

You can also choose an execution policy for the current PowerShell session only, by using a scope of “Process”, or by using the -ExecutionPolicy parameter when launching powershell.exe:

```
PowerShell.exe -ExecutionPolicy Bypass -File c:\some\script.ps1
```

---

<div class="post-metadata">

### Author: ![alexander-johansson](https://avatars.discourse-cdn.com/v4/letter/a/f05b48/32.png) [@alexander-johansson](https://forums.powershell.org/u/alexander-johansson)
#### Post date: [July 14, 2014, 6:48am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/16 "2014-07-14T06:48:41Z")

</div>

If you are in an Active Directory environment you should set the exectuion policy by a Group Policy, the policy is located as seen in the picture below.

/Alexander

---

<div class="post-metadata">

### Author: ![mr-jacko](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mr-jacko](https://forums.powershell.org/u/mr-jacko)
#### Post date: [July 14, 2014, 7:01am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/17 "2014-07-14T07:01:51Z")

</div>

Are all in AD.  
Only one group of users is in workgroup.  
On these I get the following error (just because I not run the command manually as administrator):  
Set-ExecutionPolicy: Access is denied to the key 'HKEY\_LOCAL\_MACHINE \ SOFTWARE \ Microsoft \ PowerShell \ 1 \ ShellIds \ Microsoft.PowerShell 'registry.  
To change the default execution policy scope (LocalMachine)  
start Windows PowerShell with the “Run as administrator”. for  
change the execution policy for the current user, run  
“Set-ExecutionPolicy-Scope CurrentUser”.  
In C: \ Users \ Walter \ Desktop \ test\_mail.ps1: 1 car: 1

- Set-ExecutionPolicy ‘Unrestricted’-Force
- ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
  - CategoryInfo: PermissionDenied: (🙂 [Set-ExecutionPolicy], A  
uthorizedAccessException
  - FullyQualifiedErrorId: System.UnauthorizedAccessException, Microsoft.Pow  
erShell.Commands.SetExecutionPolicyCommand

my ultimate goal is just to run powerrshell script as administrator without start manually Windows PowerShell with the “Run as administrator”

---

<div class="post-metadata">

### Author: ![sam-boutros](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/sam-boutros/32/5_2.png) [@sam-boutros](https://forums.powershell.org/u/sam-boutros)
#### Post date: [July 14, 2014, 7:09am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/18 "2014-07-14T07:09:05Z")

</div>

You can use this script to set Execution Policy on all the computers in the $Targets list to “unrestricted”

```
$DomainAccount = "domain\user-with-enough-permissions-for-task"
    $Targets = @("PC1","PC2","PC3") # or get Target computer list from AD, or network scan, or . . .
    #
    if (!(Test-Path -Path ".\DomainCred.txt")) {
        Write-Output "Error: missing encrypted pwd file .\DomainCred.txt, enter the pwd to be encrypted and saved to .\DomainCred.txt for future script use:" 
        Read-Host 'Enter the pwd to be encrypted and saved to .\DomainCred.txt for future script use:' -AsSecureString | ConvertFrom-SecureString | Out-File .\DomainCred.txt
    }
    $Pwd = Get-Content .\DomainCred.txt | ConvertTo-SecureString
    $DomainCred = New-Object System.Management.Automation.PSCredential($DomainAccount,$Pwd)
    foreach ($Computer in $Targets) {
        Invoke-Command -ComputerName $Computer -Credential $DomainCred -ScriptBlock { 
            Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force} 
        Write-Output "Setting Execution Poliy on $Computer to 'Unrestricted' . . . done"
    }
```

All you have to do is:

1. Copy the script and paste it in Powershell\_ise (running in elevated permissions)
2. Edit the first 2 lines to put in an account with sufficient permissions for the task, and edit the Target list in line 2

That’s it, click the run button.

How does this work on computers with “restricted” execution policy you ask?  
Because this is not running as a script. This is running as a script-block, which is not subject to script execution policy…  
This will even work if run from a computer that’s not joined or part of the Target domain, that’s why it includes code to ask for and use separate domain credentials.

Hope that helps…

---

<div class="post-metadata">

### Author: ![mr-jacko](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@mr-jacko](https://forums.powershell.org/u/mr-jacko)
#### Post date: [July 14, 2014, 7:30am UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/19 "2014-07-14T07:30:48Z")

</div>

I’ll try.  
It seems a little complicated for my skills …  
Thanks

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:47pm UTC](https://forums.powershell.org/t/powershell-ruan-as-administrator/2790/20 "2024-05-16T20:47:47Z")

</div>


