Get-ChildItem recursively but exclude files in the parent folder - powershell

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

Related

My PowerShell script

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

exclude multiple folder using gci

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"

Get only leaf nodes from Get-ChildItem

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}

Remove-Item : Only removing filetypes, keeping folder structure intact

I'm trying to delete "only" specific filetypes in a folder and all sub-folders, but keeping all sub-folders intact.
i'm a bit of a novice, but I figured both of these were correct, but they spit out errors instead:
get-childitem $sourceDir -exclude .dll,.lib -recurse | remove-item
Fails with this error: remove-item : Windows PowerShell is in NonInteractive mode. Read and Prompt
This one:
#Remove-Item $sourceDir -recurse -exclude .dll,.lib | Where { ! $_.PSIsContainer }
Simply does nothing.
Try this for PS V2:
$excluded = #("*.dll", "*.lib")
get-childitem -path $sourceDir -exclude $excluded -Recurse | where { ! $_.PSIsContainer } | Remove-Item -Force
Or this for V3
$excluded = #("*.dll", "*.lib")
get-childitem -path $sourceDir -File -exclude $excluded -Recurse | Remove-Item -Force

Deleting all folders/files except for one folder and all root-files

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