0

I'm new to PowerShell. I want to do something like this on shutdown before turning off the computer. I plan to have Windows MMC/Local Group Policy Editor do it with a PowerShell script.

cmd as admin
echo "SOME COMMAND"
echo "exit"

Basically, after triggering the shutdown or reboot event it should:

  1. Open CMD as admin (and automatically select Yes at UAC?).

  2. Execute SOME COMMAND (there will be many).

  3. Close CMD and then allow Windows 10 to shutdown/reboot.

Now, I know how to get MMC to add a PowerShell script to execute before the shutdown command is completed. I just need to know the syntax on how to do the above three steps.

To be clear though:

My main point is to be able to open CMD as admin, type in a command and then exit CMD. PowerShell does not normally execute a few particular commands as admin correctly, but CMD does. In that way, PowerShell itself does not need admin privileges, just the temporary run CMD.

I dont' mind clicking YES on a UAC, when needed, because I figure if it comes down to it, I can probably just used sendkeys {TAB}{TAB}{ENTER} or something. What I can't seem to find on the interwebs is how to start a PowerShell script so that one can save it. I'm seeing a lot on sendkeys in syntax though. In bash in Linux, it's simple: #!/bin/bash and presto insta-bash, but Windows seems bass-ackwards in this regard.

If there is a better way to do this than, CMD > Echo "stuff" > exit CMD, please let me know. I know what stuff I want to type in each Echo "stuff" (or rather, sendkeys, if I end up using that), I just don't know how to open CMD as admin.

Thanks.

1 Answer 1

1

I really don't understand what you are after.

Yet, PowerShell's goals is not a GUI automaton tool. You can do some of that using SendKeys, just as you could via VBScript.

Yet, since you are new, it's vital that you get up to speed on PowerShell to ensure you truly understand it as well as when and why to use it. Hit up YouTube and search for 'Beginning PowerShell' and do a web search for PowerShell using SendKeys --- example ---

As for your use case:

Basically, after triggering the shutdown or reboot event it should:

  1. Open CMD as admin (and automatically select Yes at UAC?).

Starting PowerShell is a PowerShell command prompt. There is no need to open cmd.exe to use it. PowerShell can replace all that cmd.exe does. Accepting UAC is only a thing if you are trying to run an elevated command. PowerShell will not skip UAC because you have to provide admin creds. Windows requires that.

  1. Execute SOME COMMAND (there will be many).

Just make a script with the commands you want.

  1. Close CMD and then allow Windows 10 to shutdown/reboot.

PowerShell will exit by default once the script is done. Unless you specifically tell it not to.

What you are seeming to want to have is logoff and logon scripts. That is a matter of writing those and adding that to the logon/logoff Group or local machine Policy settings.

Use PowerShell and Group Policy for Your Logon Script

Using Group Policy to Deploy a Windows PowerShell Logon Script

*Update based on the thead follow-ups below *

What you are seeming to want to have is logoff and logon scripts. That is a matter of writing those and adding that to the logon/logoff Group or local machine Policy settings. You can set the same things for shutdown. Remember that a reboot is going to cause a logon action. It too has a policy setting. PowerShell console will run any command the cmd.exe can. You do have to call them correctly. If UAC is an issue, then disable it all together. Though that means you are accepting all security risks by doing so.

Accepting UAC is only a thing if you are trying to run an elevated command. PowerShell will not skip UAC because you have to provide admin creds. Windows requires that.

That is what Start-Process -Verb RunAs is for and it has a credential switch, but you still have to provide that. So, you still get prompted. You can save creds to a secure file for reuse, but have to do that before you run any scripts.

You can try the following at your own risk.

https://docs.microsoft.com/en-us/previous-versions/technet-magazine/cc162321(v=msdn.10)

https://github.com/sushihangover/SushiHangover-PowerShell/tree/master/modules/PSUserTools

(specifically the script named Start-ProcessAsAdministrator.ps1)

8
  • "Starting PowerShell is a PowerShell command prompt. There is no need to open cmd.exe to use it. PowerShell can replace all that cmd.exe does." - - - For some reason, PowerShell does not execute certain commands properly. I don't know why, but CMD does. Some examples that come to mind are the myriad of possible commands regarding slmgr and especially it's DNS options... I get weird errors with it, even if admin. But with CMD, it does just fine (sometimes not running admin). Is there a reason that I should not use PowerShell to open Admin CMD (other than it can ALMOST do everything CMD does)?
    – Steven L
    Commented Apr 2, 2019 at 16:34
  • "Accepting UAC is only a thing if you are trying to run an elevated command. PowerShell will not skip UAC because you have to provide admin creds. Windows requires that." - - - - I'm not particularly concerned with this. Even if it means that I have to use a third party program to tell the mouse where to left click (probably just use sendkeys), but I noted this already. On a side note, if you do know of a way to bypass UAC for Admin CMD, would that be possible to script?
    – Steven L
    Commented Apr 2, 2019 at 16:38
  • "What you are seeming to want to have is logoff and logon scripts. That is a matter of writing those and adding that to the logon/logoff Group or local machine Policy settings." - - - - Actually, this has nothing to do with Logging Off/On. It has to do with Shutdown/Reboot. There are no other users on the machines. So, instead of having to perform a restart in the middle of workflow, this script will start at Shutdown/Reboot, when they will be doing so anyway. NOT interrupting workflow is key here... If this cannot be run at Shutdown/Reboot, then the whole thing is useless.
    – Steven L
    Commented Apr 2, 2019 at 16:42
  • You can set the same things for shutdown. It too has a policy setting. PowerShell console will run any command the cmd.exe can. You do have to call them correctly. If UAC is an issue, then disable it all together. Though that means you are accepting all security risks by doing so.
    – postanote
    Commented Apr 2, 2019 at 23:29
  • Thanks. That's helpful. Now, do you know the syntax to call CMD as admin on order to get that UAC to open the CMD?
    – Steven L
    Commented Apr 5, 2019 at 18:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .