powershell.exe -WindowStyle Hidden seems to be broken

Mindaugas Valentinas 20 Reputation points
2024-04-12T08:25:42.8233333+00:00

I have a Powershell VB script that prompts users in the company to restart their machines and is iniciated by scheduled task:

action-> Start Program-> Powershell.exe

Argument: -WindowStyle Hidden -ExecutionPolicy bypass -File C:\Programs\script.ps1 Since it was initiated by administrators group it worked flawlessly

Once i started testing users with no elevated privileges, i notices that they are not getting prompt, since it targets only administrators group.

by mowing scheduled task to users group, script could not be initiated, since they don’t have elevated access and it breaks VB script.

I found a work around with PowerShell script argument:

Argument: -WindowStyle Hidden -ExecutionPolicy Unrestricted -File C:\Programs\script.ps1

For some reason argument “-WindowStyle” is always ignored in users group, doesn’t matter with argument I use (min/max/hidden)

Any one have faced this issue before?

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 36,401 Reputation points
    2024-04-12T20:22:19.3433333+00:00

    I have a Powershell VB script

    There is no such thing as a "Powershell VB script". There are Powershell scripts, with a .ps1 file extension, and VB scripts, with a .vbs file extension. I am going to assume that you are not doing anything with VB.

    Here is the script that I tested with on Win10/PS5.1. It demonstrates how to test to see if the user/script is running elevated so that it doesn't execute cmdlets that will fail due to not being an admin.

    start-transcript -path c:\temp\Test3.log
    Add-Type -AssemblyName System.Windows.Forms
    $msgs = @()
    function LogIt($msg){
    	$script:msgs += $msg
    	$msg
    }
    "Test3.ps1 starting."
    Logit ("Running on {0} as user {1}" -f $env:COMPUTERNAME, $(whoami.exe))
    Logit ("Execution policy is {0} " -f (get-executionpolicy))
    $admin = (whoami.exe /groups | select-string S-1-5-32-544 ) -ne $null
    		
    if ($admin) {	
        Logit "You are a member of the Administrators group."
    	$Elevated = (whoami.exe /all | select-string S-1-16-12288) -ne $null
    	if ($Elevated){
    		Logit 'This process is running elevated.'
    		
    		###############################
    		#####  Add admin commands here.		
    		###############################
    	} else {
    		Logit 'But this process is not running elevated. '
    		Logit "If you encounter errors, try with 'Run as administrator'"	
    	}
    } else {
        Logit "You do not have admin access."
    }
    $z = [System.Windows.Forms.MessageBox]::Show($msgs -join "`n")
    "Test3.ps1 ending." 
    stop-transcript
    

    I defined the scheduled task to run as the Builtin\Users group and set the trigger to the "Log on of any user".

    User's image

    When any user logs on, I see a Powershell window briefly pop up and then go away due to using -Windowstyle Hidden.

    When a non-admin logs on, it shows this msgbox.

    User's image

    Depending on the "Run with highest privileges" check box, an admin gets one of these.

    User's image

    User's image

    If you get different results, check the transcript log file and see if you have a typo or something else.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.