How to take ownership of many user profiles roaming at the same time using powershell
I am trying to set ownership of many folders in a directory with a specific modified date, for that I need to use Foreach-Object. I was able to get all the folder names I want, however I cannot use the "takeown" and "icacls" command line commands on the folders.
Here's what I've tried:
Get-ChildItem D:\2008_Profile |
?{$_.LastWriteTime -lt (Get-Date).AddDays(-163)} |
ForEach-Object {
echo $_.Name
takeown /f $_.Name /R
icacls $_.Name /grant administrators:F
}
This is what I get:
aan.V2
takeown : ERROR: The system cannot find the file specified.
At line:4 char:1
+ takeown /f $_.Name /R
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (ERROR: The syst...file specified.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Successfully processed 0 files; Failed processing 1 files
icacls : aan.V2: The system cannot find the file specified.
At line:5 char:1
+ icacls $_.Name /grant administrators:F
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (aan.V2: The sys...file
specified.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
note: aan.V2 is the folder name
+3
source to share