Invoke-HKCUSettingsForAllUsers expanded use cases – Rename Function?

//Invoke-HKCUSettingsForAllUsers expanded use cases – Rename Function?
Invoke-HKCUSettingsForAllUsers expanded use cases – Rename Function? 2017-09-21T13:19:27+00:00

The Toolkit Forums General Invoke-HKCUSettingsForAllUsers expanded use cases – Rename Function?

  • Author
    Posts
  • Mark
    Participant
    Post count: 6
    #2301 |

    Since Invoke-HKCUSettingsForAllUsers just executes a scriptblock for every user profile fed into it, and since it references a $UserProfile object with properties NTAccount, SID, and ProfilePath, it can be used for much more than just setting registry keys. Off the top of my head I can think of using it with:
    Set-RegistryKey
    Remove-RegistryKey
    Copy-File
    Remove-File
    Remove-Folder
    basically anything that you can put $UserProfile.ProfilePath into.
    Since this all works just fine, can anyone think of any reason why this function should not be named something like Invoke-ScriptBlockForAllUsers? It allows one to do rather a lot more than just set registry values… Rather nice.

    Does anyone have any interesting ways they’ve used this function?

  • Jim
    Participant
    Post count: 43

    Have you actually tried using that function with anything other than registry values? I looked at that function in the AppDeployToolkitMain.ps1 file and it only references registry info- reg.exe, ntuser.dat files, HKEY_Users etc. I tried using that function to copy a file today and I couldn’t get it to work… To copy or remove files or folders, you’d probably need to do something like this:

    [string[]]$ProfilePaths = Get-UserProfiles | Select-Object -ExpandProperty 'ProfilePath'
    foreach ($ProfilePath in $ProfilePaths) {
    Copy-File -Path "$dirFiles\discovery925_ug_en.pdf" -Destination "$ProfilePath\Desktop"
    }

  • Mark
    Participant
    Post count: 6

    Yes. I’ve used it for other things. Since the $UserProfile object that it references contains the ‘NTAccount’,’SID’,and ‘ProfilePath’ properties, anything you can use with those (mainly ‘ProfilePath’, I think) is fair game 😉

    Something like this works just fine:

    <code>[scriptblock]$stuffToDo = {
       Copy-File -Path $PathToFile -Destination $($UserProfile.ProfilePath)\AppData\SomeFolder\
    }
    Invoke-HKCUSettingsForAllUsers -RegistrySettings $stuffToDo</code>

    Since this is just a scriptblock that gets run ‘ForEvery’ user profile, and the $UserProfile object is set up internally, you can put anything you want in that scriptblock and it will get run once for every profile. Since the $UserProfile.ProfilePath property is already available then, anything that uses it will get the path of each user profile in turn.

    • This reply was modified 1 month, 3 weeks ago by  Mark. Reason: Still can't get quotes to work properly
  • Jim
    Participant
    Post count: 43

    Hmmm… Interesting… It does work! But it’s still loading/unloading the ntuser.dat for each user unnecessarily. I think I like my way better for copying/deleting files/folders… But I learned something… Thanks.

You must be logged in to reply to this topic.