title: Powershell to take ownership and delete a folder based on samaccountname
body: <p>Need to take ownership/delete folders/files in a unc path based on samaccountname</p>
  <pre><code>Foreach ($line in $users){
    Get-Aduser -identity $line.DistinguishedName | Select samaccountname
    $username = $line.samaccountname
    takeown /a /r /d y /f &quot;\\fileserver\share\$username&quot;
    Remove-Item &quot;\\fileserver\share\$username&quot;}
  </code></pre>
  <p>The above attempts to take ownership of everything in \\fileserver\share and I need it to only take ownership/remove the $username folder/subfolders. Also, need to be able to do a -whatif properly to see what will happen before it does.  Thank you!</p>