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"
Related
I need to get a list of files in all subdirectories of $PSScriptRoot but exclude any files in the parent folder of $PSScriptRoot.
$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse | Where-Object {!$_.PSIsContainer}
You could list the parent folders first, then recurse each directory for files:
$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Directory | Get-ChildItem -File -Recurse
You can add a Where-Object clause to filter out all files that are directly inside the $PSScriptRoot:
$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse -File | Where-Object { $_.DirectoryName -ne $PSScriptRoot}
you can use switch -File instead of Where-Object {!$_.PSIsContainer} as of PowerShell version 3.0
You just need to call Get-ChildItem again as a pipe
$Files = Get-ChildItem -LiteralPath $PSScriptRoot -Recurse | Where-Object {!$_.PSIsContainer} | Get-ChildItem
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
I would like to filter out only leaf nodes (folders) from Get-ChildItem, those ones that do not contain any other folders in them.
Here is my current query:
Get-ChildItem -Recurse -Directory -Exclude "*SubStr*"
He was on the right path, but just forgot the -Directory on the second Get-ChildItem command.
Get-ChildItem -Recurse -Directory -Exclude "*SubStr*" | Where-Object { -not (Get-ChildItem $_.FullName -Directory) }
Note: If you want to find hidden folders you will have to use -Force on both Get-ChildItem commands in the line below.
You would need another filter to establish whether or not the folder has anything inside it:
Get-ChildItem -Recurse -Directory -Exclude "*SubStr*" | Where-Object { -not (Get-ChildItem $_.FullName) }
Get-ChildItem -Recurse -Directory -Exclude "*SubStr*" |?{$_.psiscontainer}
I have subfolders, and subsubfolders. In the subsubfolders, I want to find all subfolders without a file named PKA.dump. Can this be done in powershell?
The subfolders go from Angle1, Angle2, etc up to Angle24
The subsubfolders go from 1eV, 2eV, to 150eV.
I can find when they are less than a certain size:
Get-Childitem -path . -filter "PKA.dump" -recurse | where {$_.Length -le 500}
But what if they dont exist?
If you have just 2 levels of directories, don't recurse. Do something like this instead:
Get-ChildItem -Path . -Directory | Get-ChildItem -Directory | ? {
-not (Test-Path -LiteralPath (Join-Path $_.FullName 'PKA.dump'))
}
For a deeper folder structure this should be ok:
Get-ChildItem -Path C:\yourpath\ -recurse | where {$_.psiscontainer} | % {
if((Get-ChildItem -Path $_.FullName -File).name -notcontains "pka.dump"){ $_.FullName }
}
I've had some months away from Powershell so excuse me if this is obvious..
I have a directory containing hundreds of filed and folders, and I would like to delete everything EXCEPT for all files in the root directory and one of the subfolders.
I will be creating a scheduled task to do this once a week.
This is what I have so far:
get-childitem -recurse | ?{ $_.psiscontainer } | remove-item
but it is quite obvious that I am only deleting folders..
Do you want to keep the contents of the one subfolder or just the subfolder itself? For the former do this:
Get-ChildItem 'C:\foo' -Exclude 'subfoldername' |
Where-Object { $_.PSIsContainer } |
Remove-Item -Recurse -Force
For the latter delete the contents of the subfolder in a second step:
Get-ChildItem 'C:\foo' -Exclude 'subfoldername' |
Where-Object { $_.PSIsContainer } |
Remove-Item -Recurse -Force
Get-ChildItem 'C:\foo\subfoldername' | Remove-Item -Recurse -Force
With PowerShell v3 and newer you can replace the Where-Object filter by adding a parameter -Directory to Get-ChildItem:
Get-ChildItem 'C:\foo' -Exclude 'subfoldername' -Directory |
Remove-Item -Recurse -Force
Try this
get-childitem -exclude SubFolderName -recurse | ?{ $_.psiscontainer } | remove-item
This command will do what you are looking for. From what I have read, the Remove-Item is buggy.
Get-ChildItem -Path D:\Temp -Recurse | Remove-Item -Exclude W3SVC5 -Recurse -force