How To Run A Powershell Script For All Users

AcidWine 21 Reputation points
2021-02-03T20:59:34.563+00:00

I want a method to run my script locally for all local users on the machine, please list the ways to do this if you can, I have researched in many places and most of the information is incomplete, if at least this topic can be more complete, it would help many other people as well.

As a example for one of my needs is to change the desktop wallpaper of all users, i already have the script, and i use a tool to execute it remotly as elevated user, but its only gonna change for that user0. Not only for visual changes, but if i need to execute something that is needed to be run for all users, i dont want to "deploy" the script in the computer, letting it be there until is executed for each user in login as in GPO or AD script or scheduled task, i want it to made the changes for all users in one only execution, without the need of others credentials, like the way you can add a profile WiFi with powershell for any local user, if youre a elevated user.

As said before, if you can list the methods even if it is one that i mentioned in my problem that doesn't serve my purpose, still comment it please.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,396 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 32,091 Reputation points
    2021-02-04T16:57:01.307+00:00

    The only way that I know to do that is to run a "reg load" command and point it to the ntuser.dat for each user. The main problem is that the registry gets loaded as a subkey of HKLM. You can programmatically make changes, but if you run a program that references HKCU, that won't work.

    Here's a script that displays every users wallpaper. You can add code to change the value.

    # Refer to this site
    # https://devblogs.microsoft.com/scripting/update-or-add-registry-key-value-with-powershell/
    
    cls
    # First test loaded hives.
    $AllUsers = Get-ChildItem REGISTRY::HKEY_USERS                      
    foreach ($u in $Allusers) {
        $u.Name
        $r =  "REGISTRY::$($u.name)\Control Panel\Desktop" 
        $wp = (get-itemproperty -path $r -name Wallpaper -ErrorAction SilentlyContinue).wallpaper  
        if ($wp) {
            $wp                         # Display wallpaper file name 
            # TODO: Here is where you test to see if the value needs changed or not and make the change. 
        } else {
            "Wallpaper not found."
        }
    
        ""
    }
    
    # Step 2 is to load each ntuser.dat using "reg load"  
    # Note that this registry cannot be referenced as HKEY_CURRENT_USER
    # This script will load it into HKEY_LOCAL_MACHINE\ThisUser 
    
    $users = Get-ChildItem -path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" 
    foreach ($u in $users ) {
        ""
        $r = "registry::" + $u.name
        $ntuser = (get-itemproperty -path $r  -name ProfileImagePath).ProfileImagePath 
        $ntuser = $ntuser + "\ntuser.dat"    
        $ntuser
        if ((test-path -Path $ntuser)) {
        } else {
            "No user registry"
            continue
        }
    
        $output =    reg.exe load HKLM\ThisUser $ntuser 2>&1
        if ($LastExitCode -ne 0) {
            "Didn't work, user must be logged on."
            #$output 
            $output = reg.exe unload HKLM\ThisUser 2>&1
            continue  
        }
    
        $r =  "HKLM:\Thisuser\Control Panel\Desktop" 
        $wp = (get-itemproperty -path $r -name Wallpaper -ErrorAction SilentlyContinue).wallpaper  
        if ($wp) {
            $wp                         # Display wallpaper file name 
            # TODO: Here is where you test to see if the value needs changed or not and make the change. 
        } else {
            "Wallpaper not found."
        }
        $output = reg.exe unload HKLM\ThisUser 2>&1
    }
    
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 31,016 Reputation points Microsoft Vendor
    2021-02-04T09:02:13.427+00:00

    Hi,

    You can try start-process

    $username = "username"  
    $password = "password"  
    $securePassword = ConvertTo-SecureString $password -AsPlainText -Force  
    $credential = New-Object System.Management.Automation.PSCredential $username, $securePassword  
    Start-Process powershell.exe -Credential $credential -ArgumentList "-file $script"  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

  2. Bill Stewart 181 Reputation points
    2021-02-05T16:50:45.383+00:00

    IMO it is far easier to change wallpaper for all users using Group Policy rather than trying to use a script.

    0 comments No comments