Query User Specific Registry Keys and Export to CSV

I have a task to query the OneDrive HKCU registry key(HKCU:\Software\Microsoft\OneDrive\Accounts) and export those key properties and its values to some network location for evaluation.

So I wrote a general PS script like below to do the job. And this needs to be executed only when user is logged in and in UserContext

Get-ItemProperty -Path HKCU:\Software\Microsoft\OneDrive\Accounts\Business1 -Name userfolder,username,useremail | Export-Csv \abcd\efgh\ijkl\OneDrive.Csv -NoTypeInformation -Append -Force

Get-ItemProperty -Path HKCU:\Software\Microsoft\OneDrive\Accounts\Personal -Name userfolder,username,useremail | Export-Csv \abcd\efgh\ijkl\OneDrive.Csv -NoTypeInformation -Append -Force

But My issue is how can incorporate this into PSADT?
OR if any users does not have those keys or properties, how to stop them from seeing some error message in PowerShell window?

If you dont want error messages occurring then use the -ErrorAction parameter to tell Powershell to Ignore or SilentlyContinue errors in the script (see hyperlink for more information).

Handling Errors the PowerShell Way

Assuming you are using the PSADT you should consider using the Get-RegistryKey function built in to the toolkit rather than Get-ItemProperty. Another toolkit function you could consider using is the “Invoke-HKCURegistrySettingsForAllUsers” function. This would allow you to run the script at the machine level rather than the user level.

I use this code to read the HKCU of all profiles on a machine. This case it is to delete some keys, but you could use it to read out the values. Users don’t need to be logged in. I use this when I deploy a new version of the drivers of 3dconnection mouse, so after every installation, the users have a clean set of registry keys.

But if you read out these values and export them to a CSV, its stored localy. The script runs under a local system account. It probably cant access shares to store the file on.

# Cleanup Profiles Registry
# Create access to HKEY_USERS
    If ( !(Get-PSDrive -PSProvider Registry -Name HKU -ErrorAction SilentlyContinue) ) {
		New-PSDrive -PSProvider Registry -Name HKU -Root HKEY_USERS

	$HkcuKeys2Del = @("Software\3Dconnexion")

	ForEach ( $Key in $HkcuKeys2Del )  {
		# Get all profile regkeys
		$ProfileKeys = Get-ChildItem -path HKU:\
		# Remove key from all profiles
		ForEach ($ProfileKey in $ProfileKeys) {
			$ProfileKey = ($($ProfileKey.Name) -replace "HKEY_USERS","HKU:")
			Remove-RegistryKey -Key "$ProfileKey\$Key" -Recurse -ContinueOnError $True