{"id":2252,"date":"2025-12-12T21:45:13","date_gmt":"2025-12-12T21:45:13","guid":{"rendered":"https:\/\/stevenwhiting.com\/blog\/?p=2252"},"modified":"2025-12-12T21:45:13","modified_gmt":"2025-12-12T21:45:13","slug":"powershell-blank-ad-profile-path","status":"publish","type":"post","link":"https:\/\/stevenwhiting.com\/blog\/?p=2252","title":{"rendered":"Powershell &#8211; Blank AD Profile PATH"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>&lt;#\n.SYNOPSIS\n  Clear Home Folder (local path) for each AD user in an OU and export results to CSV.\n\n.PARAMETER SearchBase\n  Distinguished Name (DN) of the OU to search.\n\n.PARAMETER WhatIfMode\n  If supplied, no changes are actually made.\n\n.PARAMETER CsvPath\n  Output path for the CSV file.\n\n#>\n\nparam(\n    &#91;string]$SearchBase = 'OU=.Users (Role Based),OU=Steven Whiting,DC=whiting-steven,DC=co,DC=uk',\n    &#91;switch]$WhatIfMode,\n    &#91;string]$CsvPath = \".\\HomeFolder_Clear_Report_$(Get-Date -Format yyyyMMdd_HHmmss).csv\"\n)\n\n# Import AD module\nImport-Module ActiveDirectory -ErrorAction Stop\n\nWrite-Output \"SearchBase: $SearchBase\"\nif ($WhatIfMode) { Write-Output \"WHATIF MODE ENABLED \u2014 no changes will be made.\" }\n\n# Retrieve users\n$users = Get-ADUser -Filter * -SearchBase $SearchBase -SearchScope Subtree -Properties homeDirectory,homeDrive,DistinguishedName,SamAccountName\n\nif (-not $users) {\n    Write-Output \"No users found under the specified OU.\"\n    exit\n}\n\n# Results array\n$results = @()\n\nforeach ($u in $users) {\n    $result = &#91;PSCustomObject]@{\n        SamAccountName = $u.SamAccountName\n        DistinguishedName = $u.DistinguishedName\n        PreviousHomeDirectory = $u.homeDirectory\n        PreviousHomeDrive = $u.homeDrive\n        Action = \"\"\n    }\n\n    if ($WhatIfMode) {\n        $result.Action = \"Would Clear\"\n        $results += $result\n        continue\n    }\n\n    try {\n        Set-ADUser -Identity $u.DistinguishedName -Clear homeDirectory,homeDrive -Confirm:$false -ErrorAction Stop\n        $result.Action = \"Cleared\"\n    } catch {\n        $result.Action = \"Failed: $($_.Exception.Message)\"\n    }\n\n    $results += $result\n}\n\n# Export CSV\n$results | Export-Csv -NoTypeInformation -Path $CsvPath\n\nWrite-Output \"CSV report created: $CsvPath\"\nWrite-Output \"Completed.\"<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[346],"tags":[333],"class_list":["post-2252","post","type-post","status-publish","format-standard","hentry","category-powershell","tag-powershell"],"_links":{"self":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2252"}],"version-history":[{"count":1,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2252\/revisions"}],"predecessor-version":[{"id":2253,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2252\/revisions\/2253"}],"wp:attachment":[{"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/stevenwhiting.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}