
{"id":1319,"date":"2014-11-11T20:56:06","date_gmt":"2014-11-11T20:56:06","guid":{"rendered":"http:\/\/tech-no.104.210.61.21.xip.io\/?p=1319"},"modified":"2014-11-11T20:56:06","modified_gmt":"2014-11-11T20:56:06","slug":"using-powershell-to-find-orphaned-home-folders","status":"publish","type":"post","link":"https:\/\/tech-no.org\/?p=1319","title":{"rendered":"Using powershell to find orphaned home folders."},"content":{"rendered":"<p>Recently, I was tasked with cleaning up space on the file server used to store users Home drives. In an effort to keep the drive from completely filling up, I used the following steps:<\/p>\n<ul>\n<li>I ran some scripts to find iTunes libraries, cleaned up 40GB of policy violations with that one.<\/li>\n<\/ul>\n<p><a title=\"https:\/\/svn.as35684.net\/joey\/Public\/SO\/8729014\/foo.ps1\" href=\"https:\/\/svn.as35684.net\/joey\/Public\/SO\/8729014\/foo.ps1\" target=\"_blank\">https:\/\/svn.as35684.net\/joey\/Public\/SO\/8729014\/foo.ps1<\/a><\/p>\n<ul>\n<li>Using foldersize to find the largest home folders and worked with end users to clean up another 100GB.<\/li>\n<\/ul>\n<p><a title=\"http:\/\/foldersize.sourceforge.net\/\" href=\"http:\/\/foldersize.sourceforge.net\/\" target=\"_blank\">http:\/\/foldersize.sourceforge.net\/ <\/a><\/p>\n<ul>\n<li>Next I ran this powershell scripts to audit the folders for the following:\n<ul>\n<li>no AD user account<\/li>\n<li>disabled AD user account<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><a title=\"https:\/\/gallery.technet.microsoft.com\/scriptcenter\/Script-to-get-orphaned-ea7b3075#content\" href=\"https:\/\/gallery.technet.microsoft.com\/scriptcenter\/Script-to-get-orphaned-ea7b3075#content\" target=\"_blank\">https:\/\/gallery.technet.microsoft.com\/scriptcenter\/Script-to-get-orphaned-ea7b3075#content<\/a><\/p>\n<p>I used the Export cmdlet from here: \u00a0(to save the output to a CSV)<\/p>\n<p><a title=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee176825.aspx\" href=\"http:\/\/technet.microsoft.com\/en-us\/library\/ee176825.aspx\" target=\"_blank\">http:\/\/technet.microsoft.com\/en-us\/library\/ee176825.aspx<\/a><\/p>\n<p>The outcome of this work was freeing up 500 GB of disk space!<\/p>\n<p>Here is the structure of the command I used to execute the command and send the output to CSV:<\/p>\n<blockquote><p>\u00a0.\\Get-OrphanHomeFolder.ps1 -HomeFolderPath \\\\Servername\\share\u00a0-FolderSize | Export-CSV c:\\scripts\\orphaned.csv<\/p><\/blockquote>\n<p>Here is a copy of the Powershell code to find the orphaned folders in case the link goes bad:<\/p>\n<blockquote><p>&lt;#<br \/>\n.SYNOPSIS<br \/>\nChecks if home folder still has an enabled AD account and list size of the folder<\/p>\n<p>.DESCRIPTION<br \/>\nThis script queries AD with the name of the home folder. If this query does not result in an account or a disabled account the script will list the folder size with the folder path and error message. The script will output an array of PSObject which can be piped into various Format-* and Export-* Cmdlets.<\/p>\n<p>.PARAMETER HomeFolderPath<br \/>\nThis parameter determines which folder should be scanned. A list of all folders will be checked for matching samaccountnames in Active Directory. Any folders that do not have a name that matches a samaccount in AD or that match a disabled account are listed.<\/p>\n<p>.PARAMETER FolderSize<br \/>\nThis parameter determines if the folder size should be retrieved for orphaned home folders. Not specifying this parameter will significantly increase speed of execution.<\/p>\n<p>.PARAMETER MoveFolderPath<br \/>\nSpecifying this parameter will move all orphaned folders to the specified folder.<\/p>\n<p>.PARAMETER MoveDisabled<br \/>\nThis switch parameter works in combination with the MoveFolderPath parameter, it will also move the homefolders of disabled accounts.<\/p>\n<p>.NOTES<br \/>\nName: Get-OrphanHomeFolder.ps1<br \/>\nAuthor: Jaap Brasser<br \/>\nVersion: 1.5.1<br \/>\nDateCreated: 2012-10-19<br \/>\nDateUpdated: 2013-05-24<\/p>\n<p>.LINK<br \/>\nhttp:\/\/www.jaapbrasser.com<\/p>\n<p>.EXAMPLE<br \/>\n.\\Get-OrphanHomeFolder.ps1 -HomeFolderPath \\\\Server01\\Home -FolderSize<\/p>\n<p>Description:<br \/>\nWill list all the folders in the \\\\Server01\\Home path. For each of these folders it will query AD using the foldername, if the query does not return an AD account or a disabled AD account an error will be logged and the size of the folder will be reported<\/p>\n<p>.EXAMPLE<br \/>\n.\\Get-OrphanHomeFolder.ps1 -HomeFolderPath \\\\Server02\\Fileshare\\Home | Format-Table -AutoSize<\/p>\n<p>Description:<br \/>\nWill list all the folders in the \\\\Server02\\Fileshare\\Home. Will wait until all folders are processed before piping the input into the Format-Table Cmdlet and displaying the results in the console.<\/p>\n<p>.EXAMPLE<br \/>\n.\\Get-OrphanHomeFolder.ps1 -HomeFolderPath \\\\Server02\\Fileshare\\Home -MoveFolderPath \\\\Server03\\Fileshare\\MovedHomeFolders<\/p>\n<p>Description:<br \/>\nWill list all the folders in the \\\\Server02\\Fileshare\\Home folder and will move orphaned folders to \\\\Server03\\Fileshare\\MovedHomeFolders while displaying results to console.<br \/>\n#&gt;<br \/>\nparam(<br \/>\n[Parameter(Mandatory=$true)]<br \/>\n$HomeFolderPath,<br \/>\n$MoveFolderPath,<br \/>\n[switch]$FolderSize,<br \/>\n[switch]$MoveDisabled<br \/>\n)<br \/>\n# Check if HomeFolderPath is found, exit with warning message if path is incorrect<br \/>\nif (!(Test-Path -LiteralPath $HomeFolderPath)){<br \/>\nWrite-Warning &#8220;HomeFolderPath not found: $HomeFolderPath&#8221;<br \/>\nexit<br \/>\n}<\/p>\n<p># Check if MoveFolderPath is found, exit with warning message if path is incorrect<br \/>\nif ($MoveFolderPath) {<br \/>\nif (!(Test-Path -LiteralPath $MoveFolderPath)){<br \/>\nWrite-Warning &#8220;MoveFolderPath not found: $MoveFolderPath&#8221;<br \/>\nexit<br \/>\n}<br \/>\n}<\/p>\n<p># Main loop, for each folder found under home folder path AD is queried to find a matching samaccountname<br \/>\nGet-ChildItem -LiteralPath &#8220;$HomeFolderPath&#8221; -Force | Where-Object {$_.PSIsContainer} | ForEach-Object {<br \/>\n$CurrentPath = Split-Path -Path $_ -Leaf<br \/>\n$ADResult = ([adsisearcher]&#8221;(samaccountname=$CurrentPath)&#8221;).Findone()<\/p>\n<p># If no matching samaccountname is found this code is executed and displayed<br \/>\nif (!($ADResult)) {<br \/>\n$HashProps = @{<br \/>\n&#8216;Error&#8217; = &#8216;Account does not exist and has a home folder&#8217;<br \/>\n&#8216;FullPath&#8217; = $_.FullName<br \/>\n}<br \/>\nif ($FolderSize) {<br \/>\n$HashProps.SizeinBytes = [long](Get-ChildItem -LiteralPath $_.Fullname -Recurse -Force -ErrorAction SilentlyContinue |<br \/>\nMeasure-Object -Property Length -Sum -ErrorAction SilentlyContinue | Select-Object -Exp Sum)<br \/>\n$HashProps.SizeinMegaBytes = &#8220;{0:n2}&#8221; -f ($HashProps.SizeinBytes\/1MB)<br \/>\n}<\/p>\n<p>if ($MoveFolderPath) {<br \/>\n$HashProps.DestinationFullPath = Join-Path -Path $MoveFolderPath -ChildPath (Split-Path -Path $_.FullName -Leaf)<br \/>\nMove-Item -LiteralPath $HashProps.FullPath -Destination $HashProps.DestinationFullPath -Force<br \/>\n}<\/p>\n<p># Output the object<br \/>\nNew-Object -TypeName PSCustomObject -Property $HashProps<\/p>\n<p># If samaccountname is found but the account is disabled this information is displayed<br \/>\n} elseif (([boolean]($ADResult.Properties.useraccountcontrol[0] -band 2))) {<br \/>\n$HashProps = @{<br \/>\n&#8216;Error&#8217; = &#8216;Account is disabled and has a home folder&#8217;<br \/>\n&#8216;FullPath&#8217; = $_.FullName<br \/>\n}<br \/>\nif ($FolderSize) {<br \/>\n$HashProps.SizeinBytes = [long](Get-ChildItem -LiteralPath $_.Fullname -Recurse -Force -ErrorAction SilentlyContinue |<br \/>\nMeasure-Object -Property Length -Sum -ErrorAction SilentlyContinue | Select-Object -Exp Sum)<br \/>\n$HashProps.SizeinMegaBytes = &#8220;{0:n2}&#8221; -f ($HashProps.SizeinBytes\/1MB)<br \/>\n}<\/p>\n<p>if ($MoveFolderPath -and $MoveDisabled) {<br \/>\n$HashProps.DestinationFullPath = Join-Path -Path $MoveFolderPath -ChildPath (Split-Path -Path $_.FullName -Leaf)<br \/>\nMove-Item -LiteralPath $HashProps.FullPath -Destination $HashProps.DestinationFullPath -Force<br \/>\n}<\/p>\n<p># Output the object<br \/>\nNew-Object -TypeName PSCustomObject -Property $HashProps<\/p>\n<p># Reserved for future use, folders that do have active user accounts<br \/>\n} else {<br \/>\n}<br \/>\n}<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I was tasked with cleaning up space on the file server used to store users Home drives. In an effort to keep the drive from completely filling up, I used the following steps: I ran some scripts to find iTunes libraries, cleaned up 40GB of policy violations with that one. https:\/\/svn.as35684.net\/joey\/Public\/SO\/8729014\/foo.ps1 Using foldersize to &hellip;<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/tech-no.org\/index.php?rest_route=\/wp\/v2\/posts\/1319"}],"collection":[{"href":"https:\/\/tech-no.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tech-no.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tech-no.org\/index.php?rest_route=\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/tech-no.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1319"}],"version-history":[{"count":1,"href":"https:\/\/tech-no.org\/index.php?rest_route=\/wp\/v2\/posts\/1319\/revisions"}],"predecessor-version":[{"id":1320,"href":"https:\/\/tech-no.org\/index.php?rest_route=\/wp\/v2\/posts\/1319\/revisions\/1320"}],"wp:attachment":[{"href":"https:\/\/tech-no.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tech-no.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tech-no.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}