Powershell show recursive contents for specific folder names - powershell

I am trying to write a PowerShell script that will search for all folders named "abc" within a network share. There a multiple instances of this folder located throughout this share all named "abc" in different DIR's
I would like to list the file and folder contents, of every folder named "abc" within said directory. So far I have got PowerShell to list all instances of folders named "abc" within the network drive, but I am stuck after that.
Can I pipe this into another command that will then search and list each folders contents?

You can try something like htis
Get-ChildItem -Filter "abc" -Recurse -Path "\\myserver\share" | #Find abc
Where-Object { $_.PSIsContainer } | #Get only abc-folders
Get-ChildItem -Recurse | #Search through each abc-folder
Where-Object { !$_.PSIsContainer } #Get only files
The performance would be better if you run the script locally on the server or using psremoting(ex. using Invoke-Command). The problem is that it searches through all files and folders recursively before removing the files from the search result in that first Get-ChildItem line.
If you have PS 3.0 or PS 4.0, you can also use:
Get-ChildItem -Filter "abc" -Recurse -Path "\\myserver\share" -Directory | #Find abc-folders
Get-ChildItem -Recurse -File #Get files inside those folders

Related

Powershell: Find Folders and Run Command in Those Folders

so trying to find a way to combine a couple of things the Stack Overflow crowd has helped me do in the past. So I know how to find folders with a specific name and move them where I want them to go:
$source_regex = [regex]::escape($sourceDir)
(gci $sourceDir -recurse | where {-not ($_.psiscontainer)} | select -expand fullname) -match "\\$search\\" |
foreach {
$file_dest = ($_ | split-path -parent) -replace $source_regex,$targetDir
if (-not (test-path $file_dest)){mkdir $file_dest}
move-item $_ -Destination $file_dest -force -verbose
}
And I also know how to find and delete files of a specific file extension within a preset directory:
Get-ChildItem $source -Include $searchfile -Recurse -Force | foreach{ "Removing file $($_.FullName)"; Remove-Item -force -recurse $_}
What I'm trying to do now is combine the two. Basically, I'm looking for a way to tell Powershell:
"Look for all folders named 'Draft Materials.' When you find a folder with that name, get its full path ($source), then run a command to delete files of a given file extension ($searchfile) from that folder."
What I'm trying to do is create a script I can use to clean up an archive drive when and if space starts to get tight. The idea is that as I develop things, a lot of times I go through a ton of incremental non-final drafts (hence folder name "Draft Materials"), and I want to get rid of the exported products (the PDFs, the BMPs, the AVIs, the MOVs, atc.) and just leave the master files that created them (the INDDs, the PRPROJs, the AEPs, etc.) so I can reconstruct them down the line if I ever need to. I can tell the script what drive and folder to search (and I'd assign that to a variable since the backup location may change and I'd like to just change it once), but I need help with the rest.
I'm stuck because I'm not quite sure how to combine the two pieces of code that I have to get Powershell to do this.
If what you want is to
"Look for all folders named 'Draft Materials.' When you find a folder with that name, get its full path ($source), then run a command to delete files of a given file extension ($searchfile) from that folder."
then you could do something like:
$rootPath = 'X:\Path\To\Start\Searching\From' # the starting point for the search
$searchFolder = 'Draft Materials' # the folder name to search for
$deleteThese = '*.PDF', '*.BMP', '*.AVI', '*.MOV' # an array of file patterns to delete
# get a list of all folders called 'Draft Materials'
Get-ChildItem -Path $rootPath -Directory -Filter $searchFolder -Recurse | ForEach-Object {
# inside each of these folders, get the files you want to delete and remove them
Get-ChildItem -Path $_.FullName -File -Recurse -Include $deleteThese |
Remove-Item -WhatIf
}
Or use Get-ChildItem only once, having it search for files. Then test if their fullnames contain the folder called 'Draft Materials'
$rootPath = 'X:\Path\To\Start\Searching\From'
$searchFolder = 'Draft Materials'
$deleteThese = '*.PDF', '*.BMP', '*.AVI', '*.MOV'
# get a list of all files with extensions from the $deleteThese array
Get-ChildItem -Path $rootPath -File -Recurse -Include $deleteThese |
# if in their full path names the folder 'Draft Materials' is present, delete them
Where-Object { $_.FullName -match "\\$searchFolder\\" } |
Remove-Item -WhatIf
In both cases I have added safety switch -WhatIf so when you run this, nothing gets deleted and in the console is written what would happen.
If that info shows the correct files are being removed, take off (or comment out) -Whatif and run the code again.

PowerShell GetChildItem exclude file type in base folder but include same file type from sub folder

I'm trying to find a method of getting GetChildItem to include all .xml files found in subfolders, but exclude the .xml files found in the base folder.
My folder structure looks like this:
MySubFolder\IncludeThis.xml
MySubFolder\AlsoIncludeThis.xml
AnotherSubFolder\IncludeThis.xml
AnotherSubFolder\AlsoIncludeThis.xml
ExcludeThis.xml
AlsoExcludeThis.xml
I've tried using -Include and -Exclude arguments, without any luck, as these arguments seem to only work on file types and cannot be set to work only on certain folders.
Anyone know how to get GetChildItem to filter out the .xml files from the base folder only?
PS) I won't know the names of the sub folders that exist, when using the command.
You need to get the subfolders in a first step and search them for xml Files, e.g.:
#Get list of subfolders
$folders = get-childitem -Path [path] -Directory
#Get xml files in subdirectories
$xmlFiles = get-childitem -Path $folders.fullname -Filter '*.xml' -File -recurse
While searching the the answer to this question, I came up with a method of achieving what I needed, by combining results from multiple calls to Get-ChildItem:
#Find the list of all files and folders, including files in sub folders, from a directory:
$All = Get-ChildItem -Recurse
#Find a list of items to exclude from the first list:
$Exclude = Get-ChildItem *.xml
#Remove excluded items from the list of all items:
$Result = $All | Where-Object {$Exclude.FullName -NotContains $_.FullName}
#These terms can be combined into a single difficult-to-read statement:
Get-ChildItem -Recurse | Where-Object {(Get-ChildItem *.xml).FullName -NotContains $_.FullName}

Powershell Recursive Copy Except Files found in Exclusion directory

Hi I have a folder Called "A" and folder "A" has files and sub folders within it. I also have another folder directory called "Exclusion" with some copied files and folders from "A" within it. I'm looking for a Powershell script or Command Line option that will COPY & MOVE all the objects from A that are NOT found in the Exclusion directory to a new folder directory called "Output".
Thanks,
-B
Use Get-ChildItem to get a list of files in your exclusion directory, then take only the names of the files and hold those in an array.
Optionally use New-Item with the -Force parameter to ensure that your output directory exists before sending files there.
Next use Get-ChildItem to iterate through all files in our source (A) directory, use Where-Object and the -notin operator to exclude any files which have the same names as those gathered from your exclusion directory, then use Move-Item to move the files to your destination (Output) directory.
[string[]]$filenamesToExclude = Get-ChildItem -Path 'c:\somewhere\exclusion' -Recurse | Select-Object -ExpandProperty Name
New-Item -Path 'c:\somewhere\output\' -ItemType 'Directory' -Force | Out-Null #ensure the target directory exists / don't output this command's return value to the pipeline
Get-ChildItem -Path 'c:\somewhere\A' -Recurse | Where-Object {$_.Name -notin $filenamesToExclude} | Move-Item -Destination 'c:\somewhere\output\'

PowerShell Script finding File using extension and excluding a folder

I am using the below code:
Get-ChildItem -Path N:\USERS -Filter DANTOM.DTM -Recurse -ErrorAction SilentlyContinue -Force
I need it to either find the file "DANTOM.DTM" or the extension ".DTM". I need to exclude the folder N:\USERS\EDI because it is a 1.7TB folder that would never have this file in it. So in doing so would really speed up the process.
I would like the end result to either spit into a .txt file saying which folders inside of N:\USERS has the file or just have it display as a list in powershell.
Thank you,
Assuming that the files of interest do not reside directly in N:\USERS (only in subdirs.), try the following (PSv3+ syntax); send to a file by appending > dirs.txt, for instance.
Get-ChildItem N:\USERS -Directory | ? Name -ne 'EDI' |
Get-ChildItem -Recurse -Filter *.DTM |
ForEach-Object { $_.DirectoryName }
Note: While it is tempting to try a simpler approach with -Exclude EDI, it unfortunately doesn't seem to be effective in excluding the entire subtree of the EDI subfolder.

Scan out a folder + subfolder + get the files with a specific extension in PowerShell

I have a folder named LOGFILES with a subfolder named LOGFILES_OLD.
But we need to assume that I don't know the contents of the folder.
What I'm trying to develop in PowerShell is a method to show a list of all files with a .log extension.
I have failed to get it with something I have found here:
Get-Item C:\LOGFILES -Recurse | Where-Object {$_.Extension -eq ".log"} | Format-List
Why don't you use :
Get-ChildItem "C:\LOGFILES" -include *.log -Recurse