Get List of User Profile Folders (PoSH)

Share on:

The following snippet of PowerShell will collect the list of profiles active on a system from the registry and populate an array.

I remove any profile folder that does not exist, as well as any that are within the Windows folder (e.g. NetworkService, LocalSystem), but you can modify as you see fit if that doesn't meet your needs. I use this when I need to add files, remove files, or make changes to files within a users profile.

You could just loop over C:\USERS\ subfolders and be fine 9,999/10,000 times, but this covers those circumstances where profiles have been stored elsewhere, where C: is not the drive with the profiles, or when you need to make sure you're not modifying folders that aren't true profiles.

 1<#
 2
 3*******************************************************************************************************************************
 4** All code is for demonstration only and should be used at your own risk. I cannot accept liability for unexpected results. **
 5*******************************************************************************************************************************
 6
 7Use: You're welcome to use, modify, and distribute this script.  I'd love to hear about how you're using it or modifications you've made in the comments section of the original post over at ChristopherKibble.com.
 8
 9#>
10
11$profileDirs = @()
12
13Get-ChildItem -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList" | ForEach-Object {
14	$userProfile = (Get-ItemProperty -Path $_.PSPath).ProfileImagePath
15	
16	if ($userprofile.substring(0, $($env:windir).length) -eq $env:windir) {
17		# Skipping - Profile in Windows Folder
18	} elseif (!(Get-ChildItem $userProfile -ErrorAction SilentlyContinue)) {
19		# Skipping - Folder does not exist"
20	} else {
21		$profileDirs += $userProfile
22	}
23}


No comments