Remove files in folder without removing folders - powershell

How to remove all files in folder and subfolders without removing folders itself? For example:
Remove-Item -Path C:\Users\dummyuser\foldertoclean\subfolders -recurse
I was trying with -Include and -Exclude but still havent found solution.

Just use the following easy one-liner:
Remove-Item -Path C:\Users\dummyuser\foldertoclean\* -Recurse

You can try below command
Get-ChildItem -Path C:\Users\dummyuser\foldertoclean -Include *.* -File -Recurse | foreach { $_.Delete()}

Related

Powershell Loop through folder move file, create directory if doesn't exist

Good Afternoon,
I apologize if this is a basic question, but I have been struggling with it, also still very new to Powershell.
I have a network mapped folder Z:\Test.
Under Z:\Test is multiple subfolders with the same structure. I need to loop through all of the subfolders and move all PDF files if they exist in a specific location.
Z:\Test\1\Work\PDF\*.PDF - then move
Z:\Test\2\Work\PDF\*.PDF - Move So on and so on.
I have tried the following, but like I said I have been struggling with it. Thanks any help
Get-ChildItem -Path Z:\temp\*\Work -File -Include "*.PDF" -Recurse | Copy-Item -Force -Destination Y:\Temp\*\Work
I would try something like this:
$files = Get-ChildItem -Path Z:\Temp\*\Work -File -Include "*.PDF" -Recurse
foreach ($file in $files) {
Copy-Item -Path $file.FullName -Destination Y:\Temp\*\Work -Force
}

Using Remove-Item cmdlet but excluding sub-directory

I want to remove the following files from the source, however in the source there is a sub-directory that contains files with similar names. When I run the following command it is deleting files in the sub-directory with similar file name. Is there a way to just delete the files from the source and not the sub-directory?
Example: test_1_file, test_2_file, test_3_file exists in each directory, TestFolder and TestFolder/sub
$source = testfolder
remove-item -Path $source -filter test_*_file -recurse -force
It's usually easiest to pipe the output of Get-ChildItem cmdlet into Remove-Item. You then can use the better filtering of Get-ChildItem as I think -Recurse in Remove-Item has some issues. You can even use Where-Object to further filter before passing to Remove-Item
$source = testfolder
Get-ChildItem -Path $source -Filter test_*_file -Recurse |
Where-Object {$_.Fullname -notlike "$source\sub\*"} |
Remove-Item -Force
If the files to delete:
are all located directly in $source
and no other files / directories must be deleted:
Remove-Item -Path $source/test_*_file -Force
No need for -Recurse (as #Bill_Stewart notes).
Note: For conceptual clarity I've appended the wildcard pattern (test_*_file) directly to the $source path.
Using a wildcard expression separately with -Filter is generally faster (probably won't matter here), but it has its quirks and pitfalls.

Powershell 5 Get-ChildItem LiteralPath doesn't work with Include anymore

Right now I updated to Windows 10 TH2 Build 10586 with PowerShell 5.0.10586.0
Now I got a problem with Get-ChildItem
$files = Get-ChildItem -LiteralPath $path -Force -Recurse -Include *.txt
This returns ALL files in $path even they are not .txt.
This was working before the update.
When I change it to
$files = Get-ChildItem -Path $path -Force -Recurse -Include *.txt
it works again. But that's not what I want.
Is this a bug or am I doing something wrong?
Personally, I never use -Include or -Exclude anymore. I always pipe through Where-Object. I don't know if the author of -Include and -Exclude was insane or if there's a problem with the underlying .Net provider, but they're flaky as hell.
I'm on 5.0.10240.16384.
gci -Path $path -Include *.txt -Force
Returns nothing.
gci -LiteralPath $path -Include *.txt -Force
Returns everything in $path.
gci -LiteralPath $path -Include *.txt -Force -Recurse
gci -Path $path -Include *.txt -Force -Recurse
Both return *.txt in $path and all subfolders.
So what's the proper behavior supposed to be? Does the -Recurse flag modify how -Include works? I don't know. I no longer care. I'm not going to deal with that kind of behavior. I just use this:
gci -Path $path -Recurse -Force | Where-Object { $_.Extension -eq '.txt' }
I rely on Get-ChildItem to enumerate files and folders and that's it. Just give me the objects and I'll filter them. Like all the old Remove-Item -Recurse bugs, there's something there that just doesn't work the way people expect it to.
Note that -Filter does not seem to have this issue. This works:
$files = Get-ChildItem -LiteralPath $path -Force -Recurse -Filter *.txt
Filter is also more efficient, because it is used by the underlying provider (as opposed to Include which is applied by PowerShell itself, much like a where clause added by your code).
However Filter only accepts one pattern parameter, whereas Include supports multiple patterns.
I think this is a regression. I submitted it as v5 regression: Get-ChildItem -LiteralPath -Recurse ignores -Include and gets all items

PowerShell script to move files and folders including subfolders from one location to another older than x days

I developed a PowerShell script, and it's working absolutely fine. The only challenge is the files in the subfolders are not getting moved to the destination.
get-childitem -Path "\\servername\location" |
where-object {$_.LastWriteTime -lt (get-date).AddDays(-31)} |
move-item -destination "C:\Dumps"
I am unable to customize the script further.
Don't waste your time trying to re-invent robocopy in PowerShell.
robocopy \\servername\location C:\Dumps /e /mov /minage:31
Use the -Recurse option on the Get-ChildItem command to get through to the files in the sub folders and then move each individually by piping the collection to Move-Item
Get-ChildItem -Path "C:\Test" -Recurse |
Where-Object {$_.LastWriteTime -lt (Get-date).AddDays(-31)} |
Move-Item -destination "C:\Dumps"
Here's a screenshot:
Simplification of the above
robocopy A:\ B:\ /MIR /minage:31
Where
A:\ is your source
B:\ is your destination
I needed a quick one liner to move all data off one drive onto another. This worked perfectly for me:
Get-ChildItem "E:" -Recurse | Move-Item -Destination "G:"

Get-ChildItem to Move-Item - path not found

I try to move my old logfiles to a yyyy\MM\dd folder structure by
Get-ChildItem . -Recurse -Include *.log |
Move-Item -Dest {"D:\Archive\{0:yyyy\\MM\\dd}\{1}" -f $_.LastWriteTime, $_.Name} -Force
but i get a path-not-found error.
update
The source path does not seem to be the problem. It looks like using -Force on Move-Item does not create missing destination directories.
sub question: Could the same be done without Get-ChildItem?
As far as I found the proposed way of moving logs practically interesting, I decided to complete the task:
Get-ChildItem . -Recurse -Include *.log |
Move-Item -Force -Destination {
$dir = "C:\Temp\{0:yyyy\\MM\\dd}" -f $_.LastWriteTime
$null = mkdir $dir -Force
"$dir\$($_.Name)"
}
I guess that for a source file “some.log” the destination is supposed to be something like “D:\Archive\2010\04\23\some.log” and the directory “D:\Archive\2010\04\23” actually does not exist. In this case Move-Item fails. Is this the case?