Get ChildItem from only files of specific folders - powershell

I try to export files of specific named Folders:
Get-ChildItem -Path 'C:\Test' -Name -Recurse -File > C:\Test\Test.txt
I get a list like:
content.csv
Test.txt
Folder 1\INTERESTED_FOLDER\a - Kopie.txt
Folder 1\INTERESTED_FOLDER\a.txt
Folder 1\Neuer Ordner\ttt.txt
Folder 1\Neuer Ordner - Kopie\ttt.txt
Folder 2\INTERESTED_FOLDER\b - Kopie.txt
Folder 2\INTERESTED_FOLDER\b.txt
Folder 2\Neuer Ordner\ttt.txt
Folder 2\Neuer Ordner - Kopie\ttt.txt
But what i want is:
Folder 1\INTERESTED_FOLDER\a - Kopie.txt
Folder 1\INTERESTED_FOLDER\a.txt
Folder 2\INTERESTED_FOLDER\b - Kopie.txt
Folder 2\INTERESTED_FOLDER\b.txt
I tried with -Filter "INTERESTED" etc. but then i only get
C:\Test\Folder 1\INTERESTED_FOLDER
C:\Test\Folder 2\INTERESTED_FOLDER
What i do wrong?

If I read your question correctly, you want the FullNames of the files (so their names including the path).
If that is the case, remove the -Name switch and filter on the DirectoryName property like:
(Get-ChildItem -Path 'C:\Test' -Recurse -File |
Where-Object { $_.DirectoryName -match 'INTERESTED_FOLDER' }).FullName | # also matches 'MY_INTERESTED_FOLDER_123'
Set-Content -Path 'C:\Test\Test.txt'
Alternatives for the Where-Object clause:
# also matches 'MY_INTERESTED_FOLDER_123'
Where-Object { $_.DirectoryName -like '*INTERESTED_FOLDER*' }
or if you are looking for a precise match on the complete folder name
# does NOT match 'MY_INTERESTED_FOLDER_123'
Where-Object { $_.DirectoryName -split '[/\\]' -contains 'INTERESTED_FOLDER' }

You can perform a wildcard search using the -Filter parameter:
Get-ChildItem -Path 'C:\Test' -Name -Recurse -File -Filter *INTERESTED_FOLDER* > C:\Test\Test.txt
If for example, you were interested in finding the files in INTERESTED_FOLDER but also only the files that are .txt you could do:
-Filter *INTERESTED_FOLDER*.txt

Using the -Name parameter affects your capabilities because you are returning strings instead of FileInfoObjects. You may then use Where-Object to capture everything.
Get-ChildItem -Path 'C:\Test' -Name -Recurse -File |
Where {$_ -match '\\INTERESTED_FOLDER\\'} |
Set-Content c:\test\test.txt
Note that the matching above assumes INTERESTED_FOLDER is not a direct descendent of your path. If that is a possibility, then your match expression needs to be updated to \\?INTERESTED_FOLDER\\.

Related

get-childitem find files in folders with same names

I need to check that all files in folders with the naming ".policy" is ether 'azureDeploy.parameters.json' or 'azureDeploy.json'
if i only want it to check one folder i just give the full path as here:
$azureDeployFiles = #('azureDeploy.parameters.json', 'azureDeploy.json')
(Get-ChildItem -path ..\..\fes\.policy -file -Recurse).name | Should -BeIn $azureDeployFiles
and that works. But i have multiple folders called something like '.\..\..\.policy' and i would like to check if all the folders with that naming only contains files with the naming 'azureDeploy.parameters.json' or 'azureDeploy.json'
You could specify 'only files in folders named X' with Where-Object on the Directory property:
$azureDeployFiles = #('azureDeploy.parameters.json', 'azureDeploy.json')
Get-ChildItem -path ..\..\ -Recurse -File |
Where Directory -Like "*\.policy" |
# example checking file names with -In
Foreach { $_.Name -In $azureDeployFiles }
Continuing from my comment. . .with a little help from the pipeline, we can use Where-Object to filter for directories with that specific name:
$azureDeployFiles = #('azureDeploy.parameters.json', 'azureDeploy.json')
Get-ChildItem -Path '.\MyPath' -Include $azureDeployFiles -Recurse |
Where-Object Directory -Like '*\.Policy'

Rename all files with no extension in a given directory and sub-folders using PowerShell

I need to write a script in PowerShell which renames all the files with no extension under a given directory and all subfolders. By renaming I mean to add an extension e.g. ".html" to the file which doesn't have it.
So far I've tried to build something like this:
Get-ChildItem -Path 'C:\dev\blah' -Filter *. -Recurse | ForEach-Object {
Rename-Item -Path $_.FullName -NewName ($_.FullName + ".html")
}
You need to exclude folders from the enumerated items. Use the switch -File if you're running PowerShell v3 or newer, otherwise add a Where-Object filter. Also, you don't need ForEach-Object since Rename-Item accepts pipeline input.
Get-ChildItem -Path 'C:\dev\blah' -Filter *. -Recurse | Where-Object {
-not $_.PSIsContainer
} | Rename-Item -NewName {$_.Name + ".html"}

find and recurse

I have a folder structure d:\domains\<domain>\folder1\folder2\folderx
There are maybe 20 <domain> folders, with differing levels of folders below them.
I want to search all folders for .php files, and just print the unique <domain> folders where they exit.
So for example, if there are files found in
d:\domains\domain1.com\test\test
d:\domains\domain2.com\test\test
d:\domains\domain2.com\test\help
I just want domain1.com,domain2.com to be printed. It needs to work in PowerShell v2.
I have the following, but it only prints the first domain?
Get-ChildItem -Path #(Get-ChildItem -Path D:\domains | Where-Object {$_.PSIsContainer})[1].FullName -Recurse *.php |
Select-Object -ExpandProperty FullName
Enumerate the domain folders, then filter for those of them that contain .php files.
Get-ChildItem 'D:\domains' | Where-Object {
$_.PSIsContainer -and
(Get-ChildItem $_.FullName -Filter '*.php' -Recurse)
}
If you have PowerShell v3 or newer you can use the -Directory switch instead of checking for $_.PSIsContainer:
Get-ChildItem 'D:\domains' -Directory | Where-Object {
Get-ChildItem $_.FullName -Filter '*.php' -Recurse
}
Select the Name property from the output if you want just the folder/domain names:
... | Select-Object -Expand Name

I need to find a Folder on the Network Share

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

Recursively Delete Files and Directories Using a Filter on the Directory Name

I am attempting to delete all directories, sub-directories, and the files contained in them based on a filter that specifies the required directory/sub-directory name.
For example, if I have c:\Test\A\B.doc, c:\Test\B\A\C.doc, and c:\Test\B\A.doc and my filter specifies all directories named 'A', I would expect the remaining folders and files to be c:\Test, c:\Test\B and c:\Test\B\A.doc respectively.
I am trying to do this in PowerShell and am not familiar with it.
The following 2 examples will delete all of the files that match my specified filter, but the files that match the filter as well.
$source = "C:\Powershell_Test" #location of directory to search
$strings = #("A")
cd ($source);
Get-ChildItem -Include ($strings) -Recurse -Force | Remove-Item -Force –Recurse
and
Remove-Item -Path C:\Powershell_Test -Filter A
I would use something like this:
$source = 'C:\root\folder'
$names = #('A')
Get-ChildItem $source -Recurse -Force |
Where-Object { $_.PSIsContainer -and $names -contains $_.Name } |
Sort-Object FullName -Descending |
Remove-Item -Recurse -Force
The Where-Object clause restricts the output from Get-ChildItem to just folders whose names are present in the array $names. Sorting the remaining items by their full name in descending order ensures that child folders get deleted before their parent. That way you avoid errors from attempting to delete a folder that had already been deleted by a prior recursive delete operation.
If you have PowerShell v3 or newer you can do all filtering directly with Get-ChildItem:
Get-ChildItem $source -Directory -Include $names -Recurse -Force |
Sort-Object FullName -Descending |
Remove-Item -Recurse -Force
I don't think you can do it quite that simply. This gets the list of directories, and breaks the path into its constituent parts, and verifies whether the filter matches one of those parts. If so, it removes the whole path.
It adds a little caution to handle if it already deleted a directory because of nesting (the test-path) and the -Confirm helps ensure that if there's a bug here you have a chance to verify the behavior.
$source = "C:\Powershell_Test" #location of directory to search
$filter = "A"
Get-Childitem -Directory -Recurse $source |
Where-Object { $_.FullName.Split([IO.Path]::DirectorySeparatorChar).Contains($filter) } |
ForEach-Object { $_.FullName; if (Test-Path $_) { Remove-Item $_ -Recurse -Force -Confirm } }