I'm trying to delete all of the desktop.ini files in a given directory; namely Documents\Gio. I've tried del /S desktop.ini and del /S *.ini in cmd (admin mode) and get-childitem .\ -include desktop.ini -recurse | foreach ($_) {remove-item $_.fullname} and get-childitem .\ -include *.ini -recurse | foreach ($_) {remove-item $_.fullname} in PowerShell (also admin). Neither have worked. What should I do?
The desktop.ini files contain the following:
[.ShellClassInfo]
InfoTip=This folder is shared online.
IconFile=C:\Program Files\Google\Drive\googledrivesync.exe
IconIndex=12
I move the directory from my Google Drive folder but all the folders still have the shared icons on them. I was trying to change the directory and all it subdirectories and files' ownership to a different account. I tried to do this with Google Drive but only the root directory changed ownership; I can't delete any of the files or directories therein.
del /s /a desktop.ini
See del /? for help.
I had a similar problem and here is my solution:
Get-Location | Get-ChildItem -Force -Recurse -File -Filter "desktop.ini" | Remove-Item
The first part gets the current active directory.
Get-Location
You could replace it with a path like:
"C:\Users\Chris" | Get-ChildItem -Force -Recurse -File -Filter "desktop.ini" | Remove-Item
The second part gets child items in the path.
Get-ChildItem -Force -Recurse -File -Filter "desktop.ini"
-Force -> force seeing all child items even hidden ones, most "desktop.ini" are hidden
-Recurse -> to be recursive
-File -> to get only files else it could find a folder named "desktop.ini"
-Filter "desktop.ini" -> to only get items named "desktop.ini"
The last part removes the item.
Remove-Item
Adding a -WhatIf for the first run may be safer.
Remove-Item -WhatIf
This is what I used for Windows 2012 server
Create Desktop.ini files
My desktop.ini files were created from running this script which sets default folder options
$key = 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced'
Set-ItemProperty $key Hidden 1
Set-ItemProperty $key HideFileExt 0
Set-ItemProperty $key ShowSuperHidden 1
Stop-Process -processname explorer
Remove Desktop.ini files
# Remove from your user desktop
gci "$env:USERPROFILE\Desktop" -filter desktop.ini -force | foreach ($_) {remove-item $_.fullname -force}
# Remove from default desktop
gci "C:\Users\Public\Desktop" -filter desktop.ini -force | foreach ($_) {remove-item $_.fullname -force}
This will grab every folder in your home directory, and then search that folder for desktop.ini and delete it. It should be faster than using -recurse because it won't search subfolders. It should finish neatly with no errors
foreach ($file in Get-ChildItem -path \\server\homedrivedirectory) {
Get-ChildItem $file.FullName -Filter desktop.ini -force | remove-item -force -WhatIf
}
This will grab every folder in your home directory, and then create a path for every folder to \server\homedrivedirectory\user\desktop.ini and then delete that file. It should run a little faster as it doesn't have to search each user folder but it will turn up errors for every user folder that doesn't have a desktop.ini
foreach ($_ in Get-ChildItem -path \\server\homedrivedirectory) {
$path = $_.fullname
Remove-Item "$path\desktop.ini" -Force -WhatIf
}
I've left a -whatif so you can see what it would do in your environment without it doing it. If you want it to actually delete the files remove the -whatif
With powershell, you use the Get-ChildItem (alias gci) cmdlet to retrieve all desktop.ini files and pipe it to Remove-Item (alias rm):
gci 'C:\YOURPATHTODOCUMENTS\GO' -Filter desktop.ini -Recurse | rm
Related
I am interested in finding all the directories in the C:\Users directory. I know running "Get-ChildItem C:\Users" display all the directories under the C:\Users directory.
I am trying to write a script that would delete all the directories in C:\Users except for the admin's directory. Running Remove-Item C:\Users* will probably delete all the directories and that's not what I want.
Use
Get-Help Get-ChildItem -online
You'll find the latest and full parameter infomation.in the article, you'll find -Execlude and -Name parameter.
Then use the pipeline mark combine with the remove-item cmdlet, you'll achieve your goal, below just a safe example
# This is a safe command, this will get subdirectories without -Recurse parameter
Get-ChildItem -Exclude Admin -Name | Get-ChildItem
# This is danger
Get-ChildItem -Exclude Admin -Name | Remove-Item -Force
As Mentioned Above do not Remove Public, Default, All users
So an example would be:
get-childitem -Path "C:\users" -Exclude "Administrator","Public","Default","All Users","Default User" -Force -Directory| Remove-Item -Force -Recurse
The -exclude tells it to not look for the foldernames you dont want it to
The -force says to look for hidden files and -directory says to look only at folders
Then Remove-Item -Force -Recurse the -recurse is because the directory is not empty.
I need to write a script that deletes folders with last write time ~7 Days. But keep 2 "special" folder with the content in it.
Here's my script so far:
$source = "D:\TestOrdner"
$time = (Get-Date)#.AddDays(-7)
Start-Transcript "C:\log_files\log.txt"
gci $source -Recurse | ?{$_.LastWriteTime -lt $time} | del -Force -Verbose
Stop-Transcript
My only problem is how to EXCLUDE the folders with content?
My folder to keep: D:\TestOrdner\Test.
Be careful when deleting user profile folders, so keep the -WhatIf switch until you are absolutely sure the below will not delete folders that should not be deleted.
It might be a good idea to Move these folders instead of deleting them?
Since this concerns a user profile folder where every user has his/her own folder directly under the root folder, there is no need for the -Recurse switch on Get-ChildItem
Anyhow, this should do it:
$source = "D:\TestOrdner"
$time = (Get-Date).AddDays(-7)
Start-Transcript "C:\log_files\log.txt"
Get-ChildItem $source -Directory -Exclude 'Administrator','Default','Public' |
Where-Object {$_.LastWriteTime -lt $time} |
Remove-Item -Recurse -Force -confirm:$false -WhatIf
Stop-Transcript
I'm trying to use PowerShell to copy a folder with sub-folders from our users to a small backup. These folders contain a folder called "windows" I don't want to copy.
I have tried "exclude" but can't seem to get it to work.
This is the script so far:
Copy-Item "E:\Curos folder" -Exclude 'Windows' -Destination "E:\Curos folder backup" -Recurse -Verbose
I have read other posts but don't quiet understand how it works
It's my first time working with PowerShell
You are complete right.
Actually the script it's simpler than the one I have wrote before.
Here we go:
$source = "C:\Users\gaston.gonzalez\Documents\02_Scripts"
$destination = "D:\To Delete"
$exclude = "Windows"
$folders = Get-ChildItem -Path $source | Where {($_.PSIsContainer) -and ($exclude -notcontains $_.Name)}
foreach ($f in $folders){
Write-Host "This folders will be copied: $f"
Copy-Item -Path $source\$f -Destination $destination\$f -Recurse -Force
}
I'd use something like this:
Get-ChildItem $root -Directory -Recurse | % {$_.name -ne 'Windows'} | foreach {Copy-Item "$($_.FullName)" -Destination $dest -Recurse}
I haven't tested it but it's the skeleton of something you should be able to make work, although I don't find the point of using recurse on both, Get-ChildItem and Copy-Item my advice is to use it on Get-ChildItem.
I have a source folder(c:\test*) and I want that folder copied to all folders in the same location (c:\resultaat) but only to the folders in that location that contains the name demo_profit.
I think I'm almost there, but I'm missing a small part I think.
$destination = get-item -include demo_profit* -path C:\resultaat\*
copy-item C:\test\* $destination -recurse -force
thanks for your help
The -Directory parameter of Get-ChildItem requires AFAIK PSv5
Get-ChildItem -Directory -Path 'C:\resultaat\' | ForEach-Object{
if (Test-Path (Join-Path $_.FullName 'demo_profit')){
Copy-Item C:\test\* $_.FullName -recurse -force
}
So it is not clear if you want to copy the files to a folder containing an item named demo_profit, or to folder with demo_profit in the name. The second is simple, so I'll start there:
Get-ChildItem C:\resultaat\* -Directory |
Where{$_.Name -match 'demo_profit'} |
ForEach{ Copy-Item C:\test\* -dest $_.FullName -recurse -force }
That finds any folder within c:\resultaat that has 'demo_profit' as part of its name, and copies the desired files/folders into that folder. Example: C:\resultaat\Folder1_demo_profit would have all the files from C:\temp copied into it.
If you are looking for things named demo_profit, and want to copy the files into the same folder as that item you need to use the PSParentPath property, and some wildcards.
Get-ChildItem c:\resultaat\*\*demo_profit*
This command would find a file or folder with a path such as 'C:\resultaat\Amazon\az_demo_profit'. Then to copy files into the folder containing that you would use the PSParentPath property as such:
Get-ChildItem c:\resultaat\*\*demo_profit* |
Select -Expand PSParentPath -Unique
ForEach{ Copy-Item C:\test\* -dest $_ -recurse -force }
I am trying delete all files within a folder but there is 1 folder called pictures which I would like to keep but don't know how to do that. I am using the following script , it deletes everything in a folder
if ($message -eq 'y')
{
get-childitem "C:\test" -recurse | % {
remove-item $_.FullName -recurse
}
}
One solution is to use something like:
Get-ChildItem -Path "c:\test" -Recurse | Where-Object { $_.FullName -cnotmatch "\\Pictures($|\\)" -and (Get-ChildItem $_.FullName -Include "Pictures" -Recurse).Length -eq 0 } | Remove-Item -Recurse -ErrorAction SilentlyContinue;
I suspect there must be a way more elegant way to do this. Here's what this does: it enumerates all files in the C:\test folder recursively (Get-ChildItem), then it removes all items from the result list using Where-Object where the path contains the directory to be excluded (specified using regex syntax) or when the item in question has child items that contains the file or directory to be excluded. The resulting list is fed to Remove-Item for removal. The -ErrorAction SilentlyContinue switch is applied to prevent errors being logged with recursive removal.
Get-ChildItem $PSScriptRoot -Force| Where-Object {$_.Name -ne "Pictures"} | Remove-Item -Recurse
I just tried this, and it worked for me. If you want to change what is deleted just change the "Pictures". This uses $PSScriptRoot for the path, which is the execution path of the Powershell script. You can rename that to be the path of where you want to delete.