\n Hi, and welcome to the PowerShell forum! \n\n\nDon’t apologize for being a “noob” or “newbie” or “n00b.” There’s just no need – nobody will think you’re stupid, and the forums are all about asking questions. Just ask!\nUse a descriptive subject. Don't say \"Need help\" or \"PowerShell Help\", actually summarize what the problem is. It helps the rest of us keep track of which problem is which.\nDon’t post massive scripts. We’re all volunteers and we don’t have time to read all that, nor will we copy, past…\n <\/blockquote>\n<\/aside>\n\n
<\/p>","upvoteCount":1,"datePublished":"2018-03-20T14:56:54.000Z","url":"https://community.spiceworks.com/t/powershell-getaduser-erroring-out/641447/2","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"
#*****************************************************************************\n#Created By: KV-Tools\n#Purpose: Clean home folders from main root that no longer match an AD account\n#*****************************************************************************\n\n#Imports the proper module\nimport-module ActiveDirectory\n\n#************************************************************************************************************\n#Note 1: This script is designed to run from a main directory. For example: The directory all of your users \n# are listed under could be called \"Users\" (E:\\Users\\SmithO). \"E:\\Users\" would have to be the main\n# directory. This script reads each subfolder name after the root directory to return the name\n# of the folder. Failure to run from the root will result in bad results.\n#Note 2: Your users home folder must match there sAMAcountName (AKA Logon Name). If you use a different name\n# for the home folder, do not use this script.\n#Note 3: If you want to run a simple test before running this on your entire system you can create a temp\n# root folder and create some manual home folders with some users names and few with some users\n# that are not in your AD environment. Run this against that root folder and it will check through AD\n# for the users sAMAccount name based on the folder and either move them to a destination folder if they\n# don't match AD or leave them because the home directories match existing users in AD.\n#************************************************************************************************************\n\n#How to Use:\n\n#Edit the script and you will see to variables ($FolderPath and $Destination). Next to $FolderPath you will enter the main folder \n#path for the home folders you want to check. Next to $Destination you will enter where you want the non-matching directories to \n#move into. The log paths are below that if you want to edit those as well. After you have set these settings you can run the script. \n#NOTE: You have to insure you run this script with an account that has full rights to Active Directory, the main folder path, and the destination path.\n\n#\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\n#///////////////////////Manual Entry *HERE*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\n#\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\n#Enter Main (Root) Folder Path\n#Ex1: \\\\MyServer\\Users\n#Ex2: C:\\Users\n$FolderPath = \"S:\\studentusers\\\"\n#Sets The Destination Path To Move The Old Home Folders Into\n#Ex1: \\\\MyServer\\Decommission\n#Ex2: C:\\Decommission\n#Note: Has To Be On Same Volume As $FolderPath\n$Destination = \"z:\\OLDHOMEFOLDER-03-19-18\\\"\n#Sets Log Path On Move\n$UnusedDirMoveLogPath = \"z:\\OLDHOMEFOLDER-03-19-18\\UnusedHomeDirMove_log.txt\"\n#Powershell Output Errors Path\n$OutPutErrorLogPath = \"z:\\OLDHOMEFOLDER-03-19-18\\UnusedHomeDirMove_OutputErrors_log.txt\"\n#\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\\/\n\n#Gets Each Folder In RootDirectory, which is each users home directory that is names the same name has there SamAccountName\n$folders = Get-ChildItem $FolderPath \n\n#Loops Through Each Home Directory Folder\nForEach ($folder in $folders) \n{\n\n #Searches For User\n $User = Get-ADUser -LDAPFilter \"(sAMAccountName=$folder)\" \n\n #Checks If Users Exists\n If ($User -eq $Null) \n {\n\n ###########################################\n #User NOT Found------------Moving Folder\n ###########################################\n \n #Sets Unused Home Directory\n $HomeDir = $FolderPath+\"\\\"+$folder\n #Sets Complete Destination Path\n $DestinationDir = $Destination+\"\\\"+$folder\n \n #Forces Error Checking To Output To Log and Not Console\n $ErrorActionPreference = \"Stop\"\n \n #Checks For Errors\n Try \n { \n \n #This has to be used to copy across volumes\n #Copy Items Into Destiation\n Copy-Item -Recurse $HomeDir $DestinationDir -Force\n \n }\n Catch\n {\n \n \"***********************************************************Error***********************************************************\" | Out-File $OutPutErrorLogPath -Append\n \"User: $folder\" | Out-File $OutPutErrorLogPath -Append\n \n #Logs All Errors Besides The One Above\n $_ | Out-File $OutPutErrorLogPath -Append\n \n \"***************************************************************************************************************************\" | Out-File $OutPutErrorLogPath -Append\n \n }\n \n \n ###################################################\n #Checks If Home Directory Was Copied Before Remove \n if ((Test-Path -path $DestinationDir) -eq $True)\n {\n \n #Forces Error Checking To Output To Log and Not Console\n $ErrorActionPreference = \"Stop\"\n \n #Checks For Errors\n Try \n { \n \n #Remove Home Directory\n #NOTE: DO NOT USE Remove-Item -Recurse -Force $HomeDir or you will get false positive results. \n # The results will tell you that the $HomeDir is not empty when it really is.\n cmd.exe /c RD /S /Q $HomeDir\n \n #Checks If Home Directory Was Removed On First Try\n #If Home Directory Exists Still It Will Wait 10 Seconds And Try Again\n #Does This If Folder Is Still Being Released\n if ((Test-Path -path $HomeDir) -eq $True)\n {\n #10 Seconds Sleep\n Start-Sleep -s 10\n \n #NOTE: DO NOT USE Remove-Item -Recurse -Force $HomeDir or you will get false positive results. \n # The results will tell you that the $HomeDir is not empty when it really is.\n #Remove Home Directory\n cmd.exe /c RD /S /Q $HomeDir\n \n } \n \n }\n Catch\n {\n \n \"***********************************************************Error***********************************************************\" | Out-File $OutPutErrorLogPath -Append\n \"User: $folder\" | Out-File $OutPutErrorLogPath -Append\n \n #Logs All Errors Besides The One Above\n $_ | Out-File $OutPutErrorLogPath -Append\n \n \"***************************************************************************************************************************\" | Out-File $OutPutErrorLogPath -Append\n \n }\n\n }\n ###################################################\n \n #Checks If Home Directory Was Removed \n if ((Test-Path -path $HomeDir) -eq $False)\n {\n \n #Checks If Home Directory Was Copied Into Destination Path \n if ((Test-Path -path $DestinationDir) -eq $False)\n {\n \n #Gets Current Date and Time For Log Entry\n $LogEntryDate = Get-Date –Format G\n \n #Log Source Path Was Removed\n Add-Content -Path $UnusedDirMoveLogPath -Value \"******************************************************************************************************************\"\n Add-Content -Path $UnusedDirMoveLogPath -Value \"Log Entry Date: $LogEntryDate\"\n Add-Content -Path $UnusedDirMoveLogPath -Value \"\\\\\\///User: $folder\\\\\\///\" \n Add-Content -Path $UnusedDirMoveLogPath -Value \"Error Report: Home Directory Did Not Move Correctly\"\n \n }\n Else\n {\n #Gets Current Date and Time For Log Entry\n $LogEntryDate = Get-Date –Format G\n \n #Log Source Path Was Not Removed\n Add-Content -Path $UnusedDirMoveLogPath -Value \"******************************************************************************************************************\"\n Add-Content -Path $UnusedDirMoveLogPath -Value \"Log Entry Date: $LogEntryDate\"\n Add-Content -Path $UnusedDirMoveLogPath -Value \"\\\\\\///User: $folder\\\\\\///\"\n Add-Content -Path $UnusedDirMoveLogPath -Value \"Success Report: Home Directory Path Was Moved\"\n \n }\n \n }\n Else\n {\n #Gets Current Date and Time For Log Entry\n $LogEntryDate = Get-Date –Format G\n \n #Log Source Path Was Not Removed\n Add-Content -Path $UnusedDirMoveLogPath -Value \"******************************************************************************************************************\"\n Add-Content -Path $UnusedDirMoveLogPath -Value \"Log Entry Date: $LogEntryDate\"\n Add-Content -Path $UnusedDirMoveLogPath -Value \"\\\\\\///User: $folder\\\\\\///\"\n\n Add-Content -Path $UnusedDirMoveLogPath -Value \"Error Report: Home Directory Did Not Move Correctly\"\n \n }\n \n }\n\n}\n\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-03-20T15:03:51.000Z","url":"https://community.spiceworks.com/t/powershell-getaduser-erroring-out/641447/4","author":{"@type":"Person","name":"jonathanjones7819","url":"https://community.spiceworks.com/u/jonathanjones7819"}},{"@type":"Answer","text":"Or use a regular filter<\/p>\n
Get-aduser -filter \"samaccountname -eq '$($folder.name)'\" \n<\/code><\/pre>","upvoteCount":1,"datePublished":"2018-03-20T15:07:33.000Z","url":"https://community.spiceworks.com/t/powershell-getaduser-erroring-out/641447/5","author":{"@type":"Person","name":"Neally","url":"https://community.spiceworks.com/u/Neally"}},{"@type":"Answer","text":"or simplest<\/p>\n
\n```\n$folders = Get-ChildItem $FolderPath |select -exp Name\n\n#Loops Through Each Home Directory Folder\nForEach ($folder in $folders) {\nget-aduser $folder\n}\n```\n\n<\/code><\/pre>\n$ErrorActionPreference='stop'\n$homes=GCi c:\\home\\users |select -exp name\nForeach($home in $homes){\nTry{\nget-aduser $home |Out-Null\n\n}\ncatch{\nWrite-Warning \"$Error[0] $home not found\"\nRemove-Item c:\\home\\$home -WhatIf\n}\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2018-03-20T15:12:13.000Z","url":"https://community.spiceworks.com/t/powershell-getaduser-erroring-out/641447/6","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},{"@type":"Answer","text":"Sorry, new to powershell, do you mean from this: $User = Get-ADUser -LDAPFilter “(sAMAccountName=$folder)”<\/em><\/strong> TO this: $User = Get-ADUser -Filter “sAMAccountName -eq ‘$(folder.name)’”?<\/strong><\/p>","upvoteCount":0,"datePublished":"2018-03-20T15:17:19.000Z","url":"https://community.spiceworks.com/t/powershell-getaduser-erroring-out/641447/7","author":{"@type":"Person","name":"jonathanjones7819","url":"https://community.spiceworks.com/u/jonathanjones7819"}},{"@type":"Answer","text":"