I have a PowerShell script that deletes files in directory including files in subdirectories. I need it to delete files in that directory only and not to delete files in subdirectories
Here is what I have:
$DelFiles = get-childitem $DirectoryName -include $FileTemplate -recurse |where {$_.Lastwritetime -lt (date).AddDays(-$days)}
Any help would be appreciated
Just remove -recurse:
$DelFiles = get-childitem $DirectoryName -include $FileTemplate | where {$_.Lastwritetime -lt (date).AddDays(-$days)}
Powershell (v 2.0.-1.-1) has an interesting behavior
Get-ChildItem . -include *.txt
- will fail your expectation (will return zero matches)
Get-ChildItem .\* -include *.txt
- will produce proper-expected results. (will return all txt files in current dir)
It seem to be a part of it's design - see it's manual:
Get-Help Get-ChildItem -full
Perhaps you $DirectoryName has format like C:\111\, when for your datagrab you actually should provide something like C:\111\*
get-childitem C:\111\* -include *.txt
Hope that helps. ^_^
remove recurse and add -file
if you want remove only files in directory and subdirectory
get-childitem $DirectoryName -recurse -File -Filter $FileTemplate | where {$_.Lastwritetime -lt (date).AddDays(-$days)} | Remove-Item -Force
if you want remove only files in directory and not subdirectory
get-childitem $DirectoryName -File -Filter $FileTemplate | where {$_.Lastwritetime -lt (date).AddDays(-$days)} | Remove-Item -Force
Related
I am trying to write a script in Powershell to remove some files automatically with a certain file name.
My idea is to get all the folders in the directory, then loop through the subdirectory, and remove all items with the file name, but it doesn't seem to be working as expected.
Here is my script
$folders = Get-ChildItem -path "C:\Website-Backup" -Recurse | Where-Object {$_.PsIsContainer} |Group-Object {$_.FullName.Split('_')[0] }
$subfolders = Get-ChildItem -path $folders -Recurse | Where-Object {$_.PsIsContainer} | Group-Object {$_.FullName.Split('_')[0] }
ForEach($subfolder in $subfolders)
{
Remove-Item * -Include *100x*
}
Any idea why the script doesn't seem to be doing anything?
I think you can simplify your code if I understand correctly to:
Get-ChildItem "C:\Website-Backup" -Recurse -include "*100x*" -file | remove-item
The Group-Object command is likely what's confusing things here - Remove-Item is expecting a path - you're not referencing the subfolder variable in your loop as well, so this is the same as just running the Remove-Item command as many times as there are items in the array.
You can try this instead;
Get-ChildItem -Path "C:\Website-Backup" -Recurse | Where-Object -FilterScript { $_.name -like 'MyFile.txt' } | Remove-Item
This will pipe the returned child items into Where-Object, filter it to the specified file name, then pass that to Remove-Item as a file path.
You can also skip the Where-Object, but you lose a bit of control this way;
Get-ChildItem -Path 'C:\WebSiteBackup\*MyFile.txt' -Recurse | Remove-Item
I want to move files from one location to another but not to move any files from subfolders in the directory I'm moving the files from.
I have the following code but this moves files in subfolders. Also, the extensions should only be .xml and .pdf.
This script moves all files from $MoveThese into the Destination, even the files in subfolders e.g. C:\Test\Folder1\Subfolder. Any suggestions would be helpful.
$MoveThese = "C:\Test\Folder1", "C:\Test2\Folder2", "C:\Test3\Folder3"
$Destination = "C:\Test\Docs", "C:\Test2\Docs", "C:\Test3\Docs"
For($i=0; $i -lt $FailFolders.Length; $i++){
Get-ChildItem -Path $MoveThese[$i] -Recurse -Include "*.xml", "*.pdf" | Move-Item -Destination $Destination[$i]
}
If you don't want files in subfolders, don't use -Recurse. As per Get-ChildItem documentation this "Gets the items in the specified locations and in all child items of the locations."
Missed this part of the -Include statement:
The -Include parameter is effective only when the command includes the
-Recurse parameter
Solution:
Get-ChildItem $MoveThese[$i] -File |
Where-Object {$_.Extension -in #(".xml",".pdf")} |
Move-Item -Destination $Destination[$i]
Include does not work without the -Recurse switch unfortunately unless the path uses a wildcard like in C:\Windows*
This might do it for you:
$MoveThese = "C:\Test\Folder1", "C:\Test2\Folder2", "C:\Test3\Folder3"
$Destination = "C:\Test\Docs", "C:\Test2\Docs", "C:\Test3\Docs"
For($i=0; $i -lt $MoveThese.Length; $i++){
Get-ChildItem $MoveThese[$i] -File |
Where-Object { $_.Name -like "*.xml" -or $_.Name -like "*.pdf" |
Move-Item -Destination $Destination[$i] -Force
}
How can I find Folders called BlueMountain when this folder could be nested anywhere in my Users home folder
\\Server\Users\<personsname>\
Ultimately I want to delete the folder but just to be on the safe side. The BlueMountain folder must have one of these subfolder
Certs
Config
Macros
Scripts
Spool
Traces
Transfer
This is what I have so far
Get-ChildItem -Path \\Server\Users -Recurse -Directory -Filter $_.FOLDERNAME | ForEach-Object {
If $_.FullName --eq "BlueMountain" {
}
}
You can use -recurse to look for the last thing in your path recursively. So this:
Get-ChildItem \\server\Users\BlueMountain -recurse
Will look in all subfolders of "\server\Users" for anything named "BlueMountain". Then you just need to make sure it has one of your folders.
$SubFolders = 'Certs','Config','Macros','Scripts','Spool','Traces','Transfer'
Get-ChildItem \\server\Users\BlueMountain -recurse | Where{Get-ChildItem "$($_.FullName)\*" -Include $SubFolders}
That should list only the BlueMountain folders found recursively in \server\Users which contain one of the specified subfolders. Then you can just pipe that to Remove-Item -force and call it a day. Or if you want to track things pipe it to tee-object and then to remove-item.
try this :
$SubFolders = 'Certs','Config','Macros','Scripts','Spool','Traces','Transfer'
$wordtosearch="BlueMountain"
$SearchPattern= ($SubFolders | %{ "$wordtosearch\\$_" }) -join "|"
get-childitem "\\Server\Users" -directory -Recurse |
where FullName -match $SearchPattern |
Split-Path -path {$_.FullName} -Parent |
remove-item -Recurse -ErrorAction SilentlyContinue
I have 2 subfolders 'PS Logs', 'Executables' in the root directory "C:\Temp\". How to exclude them using gci. I am doing the crude way where I exclude all the underlying files in these sub-folder.
Get-ChildItem -Path "C:\Temp\" -recurse -exclude *.*
Try this
Get-ChildItem C:\temp -Recurse | Where-Object {!($_.FullName -match 'PS Logs') -and !($_.FullName -match 'Executables')} | Select-Object -ExpandProperty Fullname
You are almost there :)
Get-ChildItem C:\Temp -Recurse -Exclude "PS Logs", "Executables"
I have a bunch of html files that I need to rename with url encoding before I upload them to the server. I've tried:
Get-ChildItem -Path c:\temp\ -Recurse -Filter *.html | Rename-Item -NewName { $_.Name.replace("*.html",[Web.Httputility]::UrlEncode("*.html")) }
But that doesn't apply the encoding, can this be even done somehow?
Here's a basic way to do it, based on this answer:
(Get-ChildItem -Path c:\temp\ -Recurse -Filter *.html -File) |
foreach { ren $_.fullname ([uri]::EscapeDataString($_))}
Originally I had written it without the () around the Get-ChildItem, but found it was still reading directory information while the rename had already renamed the first item. Then the rename reencoded the first item a second time, thus making it a bit munged up.
Here's a cleaner version that handles the files first and then folders:
$files = Get-ChildItem -Path c:\temp\ -Recurse -Filter *.html -File
$folders = Get-ChildItem -Path c:\temp\ -Recurse -Filter *.html -Directory
$files | foreach { ren $_.fullname ([uri]::EscapeDataString($_))}
$folders | foreach { ren $_.fullname ([uri]::EscapeDataString($_))}
Based on your answers i did this to make it work
(Get-ChildItem -Path c:\temp\ -Recurse -Filter *.html) |
foreach { ren $_.fullName ([uri]::EscapeDataString($_.Name))} | Out-Null
Get-ChildItem -Path c:\temp\ -Recurse | ?{ $_.PSIsContainer } |
foreach { ren $_.fullName ([uri]::EscapeDataString($_.Name))}
First I had to do only files and then folders, because when top folder gets renamed, then the path to files inside it doesn't exist any more.