Avatar of tonelm54
tonelm54 asked on

Get users profile folders location into an array

Im trying to write a script to look at the location of a users profile directories (Basically see if they are mapped correctly).


So, what I thought is collect the items I need from the registry, adding them to an array and then convert the array to a json, so like:-

$userProfile = @("Desktop","Favorites","My Music")
$userProfile.('Desktop') = Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" -Name "Desktop" $userProfile.('Favorites')  = Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" -Name "Favorites" $userProfile.('My Music') = Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" -Name "My Music" #$profileLocations[] = Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" -Name "My Pictures" #$profileLocations[] = Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" -Name "My Video" #$profileLocations[] = Get-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" -Name "Personal" $userProfile | ConvertTo-Json

Open in new window


But all I get is:-

[
    "Desktop",
    "Favorites",     "My Music" ]

Open in new window


I want the result to be json as then its easier to collect and report on (well for me anyway :-))


Any suggestions?

Powershell

Avatar of undefined
Last Comment
oBdA

8/22/2022 - Mon
oBdA

You need proper objects to convert to json; all you defined there is an array of strings.
This automates the registry queries:
$shellFolders = @(
	'Desktop'
	'Favorites'
	'My Music'
	'My Pictures'
	'My Video'
	'Personal'
)

$userProfile = [ordered]@{}
ForEach ($folder in $shellFolders) {
	$userProfile[$folder] = (Get-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders').$folder
}
[PSCustomObject]$userProfile | ConvertTo-Json

Open in new window

ASKER
tonelm54

Ok, that works, thank you.

However it doesnt work with the rest of the script as I try to collect from multiple other sources (sorry, didnt mention this in my question)

What I have is:-
$mappedDrives = Get-SMBMapping 
$mappedPrinters = Get-Printer

$userProfiles = Get-ChildItem -Path C:\Users -Directory -Force -ErrorAction SilentlyContinue | Select-Object Name
$localAdmin = Get-LocalGroupMember -Group "Administrators" 

$shellFolders = @(
   'Desktop'
   'Favorites'
   'My Music'
   'My Pictures'
   'My Video'
   'Personal'
)

$userProfile = [ordered]@{}
ForEach ($folder in $shellFolders) {
   $userProfile[$folder] = (Get-ItemProperty -Path 'HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders').$folder
}


$userProf = @("User","UserDomain","ComputerName","profile","mappedDrives")
$userProf["user"] = ($env:UserName)
$userProf["UserDomain"] = $env:UserDomain
$userProf["ComputerName"] = $env:ComputerName
$userProf["profile"] = $userProfile
$userProf["mappedDrives"] = $mappedDrives

#Final output
$userProf | ConvertTo-Json

Open in new window

ASKER CERTIFIED SOLUTION
oBdA

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck