Delete Windows Update Residuals using Disk Cleanup via Powershell - powershell

How can I delete/cleanup all the folders under "Clean System Files" option in disk cleanup via powershell? The code below wasn't able to achieve what I wanted to.
(Start-Process -FilePath "$env:systemroot\system32\cleanmgr.exe" -ArgumentList "/sagerun:1" -Wait -PassThru)
Thank you,

Have you tried the autoclean option?
cleanmgr.exe /AUTOCLEAN
It will clean below folders:
C:$Windows.~BT*
C:$Windows.~LS*
C:$Windows.~WS*
C:\ESD\Download*
C:\ESD\Windows*
C:$WINDOWS.~Q*
C:$INPLACE.~TR*
C:\Windows.old*
C:\Windows\Panther
It will also write 2 log files as following:
C:\Windows\System32\LogFiles\setupcln\setupact.log
C:\Windows\System32\LogFiles\setupcln\setuperr.log

you can delete them one by one by starting the cmd in admin mode.
below is an example to delete the $windows.~BT folder
takeown /F C:\$Windows.~BT\* /R /A
icacls C:\$Windows.~BT\*.* /T /grant administrators:F
rmdir /S /Q C:\$Windows.~BT\

Related

Robocopy script that takes ownership and gives it back

I am currently migrating from a Unix FS to a Windows FS and i need to copy user folders.
I have created a robocopy script that is very simple with a few kinks here and there.
My script:
#echo off
set /p username=Enter Username to start copying files:
echo %username%
takeown /f \UNIXFS\users$%username%* /r /D N
timeout /t 3
robocopy \UNIXFS\users$%username% \WINDOWSFS\Production$%username% /S /E /B /Z /ZB /MT:64 /V /TEE /ETA /TS /R:5 /W:1 /BYTES /X /DEBUG /LOG:C:\Robocopy%username%.log
timeout /t 3
icacls \WINDOWSFS\Production$%username%* /grant:r domain%username%:(OI)(CI)F /T
pause
I have an input section asking me which user i would like to start copying files from and too.
i take owner ship from the user as the current domain admin, copy the files and give the user full permissions on the folder.
But the ownership is still for the domain admin and not the user chosen by the inputted user.
Therefor i have created a separate single command line bat file that will take the ownership of the folder when i log in to the users account.
Take ownership script to run on for the migrated user:
takeown /F \WindowsFS\Production$%username%* /r /D Y
is there a way i can combine these two script as one and just run this as a single script.
I have tried to just combine them both together but the last takeown command like just give the user that runs the script ownership, when you have to do this for a lot of users you don't want to manually input or run the takeown script one by one on all machines.

Copy same root directory with files to multiple wildcard directories including files

I am trying to copy one folder and its contents to multiple user directories which vary depending on the username. The directory under each user will remain constant.
Here is an example of what I am trying to achieve:
xcopy "C:\OF" "C:\Users\*\AppData\Roaming" /O /X /E /H /K /S
I am trying to use a wildcard because the username is different, but xcopy apparently cannot use wildcards any longer?
The directory of "C:\OF" will have files and other nested directories and I want to place those under the "Roaming" directory.
Thank you for any help and explanation of what I am doing wrong.
Put this in a batch file:
#ECHO OFF
FOR /d %%I IN (C:\Users\*) DO (
XCOPY "C:\OF" "%%I\AppData\Roaming"/O /X /E /H /K /S
)
That should do want you want. I didn't validate your XCOPY switches because I stopped using XCOPY years ago in favor of ROBOCOPY.
This will do the trick.
#echo off
for /d %%x in (C:\Users\*) do xcopy "C:\OF" "%%x\AppData\Roaming\OF\*" /d /e
pause
EXIT

How to empty recycling bin in batch

I need a code to empty the recycling bin without conformation I have tried the simple del $Recycle.Bin but it says access denied even when elevated does any one know a code I could use.
This emptied my bin without any confirmation.
#ECHO OFF
start /b /wait powershell.exe -command "$Shell = New-Object -ComObject Shell.Application;$RecycleBin = $Shell.Namespace(0xA);$RecycleBin.Items() | foreach{Remove-Item $_.Path -Recurse -Confirm:$false}"
Above answers are ok for cmd batch files but for the new powershell there is a better way
Simply use the cmdlet
Clear-RecycleBin
Optionally you can use the -Force or -Confirm:$false parameters so it won't ask for confirmation
For more info open powershell and type
Get-Help Clear-RecycleBin
I have just found this.
erase /s/q/f "C:\$RECYCLE.BIN\*">nul
Guaranteed to delete all content in the Recycle Bin for the selected drive while leaving the folder itself intact:
C:\$Recycle Bin\>rd . /q /s
Change to the required drive
Change into the $Recycle Bin folder
Run the command rd . /q /s [remove-dir (currentdir) /quiet /subdir]
You will get an error that the current directory is still in use (because that is your current location) and can't be deleted. This is expected behaviour because I want the $Recycle Bin folder to remain.

Replace ACLs with Inherited Defaults using PowerShell

I have a number of folders within user directories that have ended up with the wrong ACLs. I would like to find a way to use PowerShell (or a regular command prompt if that is easier) to remove the existing ACL and replace it with what it should inherit from its parent folder. The trick is that only the user that owns the folder has access to it (get-acl '.\folder' returns "Attempted to perform an unauthorized operation."). These folders all sit on a Windows Server 2003 Std system.
Try this:
#You have a textfile with FOLDER-paths
#$a = Get-Content d:\list.txt
#You have an array of FOLDER-paths
$a = #("d:\mytestfolder", "d:\my2ndtestfolder")
$a | % {
#Take ownership to admin-group
& TAKEOWN /F $_ /A /R /D Y
#Reset acl to default recursively
& ICACLS $_ /RESET /T /C
}
It turns out that this was much easier to accomplish with the regular command prompt tools. The attached script did what I needed in just a couple of lines:
#Echo Off
#Echo Taking ownership of files in %1
takeown /f %1 /r /d Y /a > :nul
#Echo Restoring default ACLs in %1
icacls %1 /reset /t /c > :nul
#Echo Restoring ownership of files to %2
subinacl /file %1 /setowner=%2 > :nul
subinacl /subdirectories %1\*.* /setowner=%2 > :nul

Number of files deleted from batch file

REM Detect how many files are on the C: drive
dir /s /b C:\ |find /c "\" > NUMfiles.###
set /p count1=<NUMfiles.###
##### TEMP FILES DELETED HERE, RUN CCLEANER, RUN MBAM, ETC #####
REM Calculate Total Files Deleted
dir /s /b C:\ |find /c "\" > NUMfiles.###
set /p count2=<NUMfiles.###
set /a count3=%count1% - %count2%
echo Number of files removed: %count3%
This doesn't seem to be giving me an accurate reading. Can anyone help?
I do a manual check via command line using the 'dir /s /b C:\ |find /c "\"' before the script, and at the end. And the output from '%count3% isn't accurate from my subtraction from the manual checks. Hope you understand my question.
Yes, as snemarch montined, the fact that you list everything and temporary files could as well be added/deleted by another process meanwhile invalidate the entire effort.
On a side note, adding "/a-d" to the "dir" command would remove the directories from being listed, thus not needing VonC's "find /v "" addition to the process, if you insist on checking files only.
Could you not check file while they get deleted instead? Not sure what you use this for but you definately need to rethink the way from source, the deleting part.
My suggestion.
If you must iterate on the all content, this command line might be more precise to list the number of files (files, not directories):
dir /a /s /OG C:\ |find /v "<DIR>" | find /c "M "
Off course, this assume a dir does display 'AM ' or 'PM '.
If it does not, the following should works better:
dir /a /s /OG C:\ |find /v "<DIR>" | find /c "/"