1

I have a very strange thing happening.

If i run powershell.exe, then in the window type this command :

Measure-Command {Get-ADPrincipalGroupMembership abc00 | select "name" | out-gridview }

the command is super fast and takes around 2 seconds.

Now if i run the exact same thing from a batch file (which contains a simple CHOICE menu) like this:

powershell.exe -command "Measure-Command {Get-ADPrincipalGroupMembership abc00 | select "name" | out-gridview} 

It takes a whooping 15 seconds, making the command very heavy and annoying to use.

Why is it so slow? Could it be the CHOICE command or something? Because when i run it in an empty test.bat it's super fast...:

powershell.exe -command "Measure-Command {Get-ADPrincipalGroupMembership abc00 | select "name" | out-gridview} 

P.S.: All the other commands in my batch file are fast except the powershell ones.

Thank you very much for your time and help as always.

UPDATE : I have tried to remove the choice menu entierly and replace it with a set /p menu and it still takes forever to load...What a strange problem.

2
  • I recommend dispensing with batch files entirely and just use PowerShell. Commented Jul 10, 2017 at 14:22
  • 1
    I'd inlude -NoProfile or short -NoP after powershell.exe -NoP and use Powershell without a temporary vbs file to elevate. See my answer. Commented Jul 10, 2017 at 14:35

2 Answers 2

1

I use this batch for elevating
(BTW an other answer to ask a different question is a bit ahemm off topic)

::ElevateMe.cmd::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off & setlocal EnableExtensions DisableDelayedExpansion
net file 1>nul 2>&1 || (powershell -NoP -NoL -Ex unrestricted -Command ^
"Start-Process -Verb RunAs -FilePath '%comspec%' -ArgumentList '/c %~f0 %*'"
  goto :eof)
:: Put code here that needs elevation
Sign up to request clarification or add additional context in comments.

1 Comment

Got it, i edited my answer ;) This code works for elevating and doesn't cause slow down of powershell commands, thanks A LOT. Have a good day.
0

Strangely i have found the culprit. I had a portion at the top of my batch file to make sure it runs as administrator automatically when opening and THAT was making powershell commands slow as hell! After removing it the powershell commands run super fast.

That was the code which is very useful to me in general but now that i found it slows down powershell...not so much.

    REM  --> Check for permissions
    "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system">nul 2>NUL

    REM --> If error flag set, we do not have admin.
    if '%errorlevel%' NEQ '0' (
        echo Requesting administrative privileges...
        goto UACPrompt
    ) else ( goto gotAdmin )

    :UACPrompt
        if exist "%temp%\getadmin.vbs" (
            del "%temp%\getadmin.vbs"
            echo Failed to acquire elevated privilege.  Try saving this script and running it from your Desktop.
            echo;
            echo Press any key to exit.
            pause>NUL
            goto :EOF
        )
        echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
        echo UAC.ShellExecute "%~s0", "%*", "", "runas", 1 >> "%temp%\getadmin.vbs"

        cscript /nologo "%temp%\getadmin.vbs"
        goto :EOF

    :gotAdmin
        if exist "%temp%\getadmin.vbs" ( del "%temp%\getadmin.vbs" )
:--------------------------------------

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.