I am looking for assistance on the last part of the script - The script looks at a file and removes user profiles on win7 machines remotely. I also need it to remove the registry entry that matches up to that user in HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList
machineinfo = import-csv "C:\tools\machine.csv" -header ("Machine")
ForEach ($item in $machineinfo) {
$machine = $item.Machine
Write-Host $machine
Function GetChoice
{
#Prompt message
$Caption = "Delete Confirming."
$Message = "Do you want to delete the unused user folder?"
$Choices = [System.Management.Automation.Host.ChoiceDescription[]]`
@("&Yes","&No")
[Int]$DefaultChoice = 0
$ChoiceRTN = $Host.UI.PromptForChoice($Caption, $Message, $Choices, $DefaultChoice)
Switch ($ChoiceRTN)
{
0 {$True}
1 {break}
}
}
Function isException($Foldername)
{
Switch($Foldername)
{
"All Users"
{ $True}
"Default User"
{ $True }
"Default"
{ $True }
"LocalService"
{ $True }
"NetworkService"
{ $True }
"Administrator"
{ $True}
"Public"
{ $True}
default
{ $False}
}
}
#Get user folder
if (test-path("\\$machine\C$\Users")){
$UserParentFolder = "\\$machine\c$\Users\"
}
#set unused days
$PeriodDays = 60
$Result = @()
#get all user folders
$userFolders = Get-ChildItem -Path $UserParentFolder
Foreach($Folder in $userFolders)
{
#get lastaccesstime
$LastAccessTime = $Folder.LastwriteTime
#Get date
$CurrentDate = Get-Date
$Tim = New-TimeSpan $LastAccessTime $CurrentDate
$Days = $Tim.days
#Compare current date and lastaccesstime
If((isException $Folder.Name )-eq $false -and ($Days -gt $PeriodDays) )
{
$temp = New-Object psobject -Property @{
"FileName" = $Folder.FullName;
"LastAccessTime" = $Folder.LastwriteTime;
"UnusedDays" = $Days
}
$Result += $Temp
}
}
If($Result)
{
$Result
If(GetChoice)
{
foreach($Folder in $Result)
{
try
{
#Remove user folder
$path = $Folder.FileName
cmd.exe /c "RD /S /Q `"$path`""
# Remove Registry entries for user profiles
Get-ChildItem "HKLM:\Software\Microsoft\Windows NT\CurrentVersion\ProfileList" | foreach {
$RegPath = $_.PSPath
$RegKey = get-itemproperty -path $RegPath | select-object -ExpandProperty ProfileImagePath
if ($RegKey -like $Path) { remove-item -path $RegPath }
}
If((test-path $path) -eq $false)
{
Write-Host "Delete unused user folder $path successfully!"
}
Catch
{
Write-Error $_
}
}
}
}
}