Powershell - Get-ChildItems -recurse not working - powershell

i am new to powershell and struggling a bit.
$SOURCE="\\server\folder\sub1\sub2\sub3\sub4"
$TYPE='*.csv'
$SUB="*subx*
Get-ChildItem -recurse -path $SOURCE -include $TYPE -filter $SUB
In my head this is to search a network path folder for folders below "sub4" that are all named "subx" if that makes sense.
Then want to return a list of files that are XX*.csv (csv's that start with XX) that are present in the subfolders that exist in sub folders below these "subx" folders.
so the paths may end up being
"\\server\folder\sub1\sub2\sub3\sub4\xxxxx\yyyyy\subx\zzzzzzz\XX*.csv"
"\\server\folder\sub1\sub2\sub3\sub4\aaaaa\bbbbb\subx\ccccccc\XX*.csv"
But it doesnt work, it only returns 1 file with subx in the name ending in .csv
I can run this and it returns the list of folders/files called "subx" but it doesnt recurse through the subfolders below subx
$SOURCE="\\server\folder\sub1\sub2\sub3\sub4"
$SUB="*subx*
Get-ChildItem -recurse -path $SOURCE -filter $SUB
I can run this and it returns all files called XX*.csv in the sub folders below the $SOURCE path
$SOURCE="\\server\folder\sub1\sub2\sub3\sub4"
$TYPE='*.csv'
Get-ChildItem -recurse -path $SOURCE -include $TYPE
any tips would be appreciated!!!

Split the operation into 2 steps:
Discover the subx folders under sub4
Discover the *.csv files under each of those
Note: In the following example I'm deliberate avoiding -Include because it's terribly slow in combination with -Recurse.
Get-ChildItem -Path $SOURCE -Directory -Recurse -Filter subx |Get-ChildItem -File -Recurse -Filter *.csv

Related

Powershell copy file/folder based on keyword

I want to copy folder which match with the keyword. however i want powershell read the keyword from starting point. i added my script below
if any folder name contain test at the start, script will copy the folder. but it's coping all folder even if "Test" keyword is available in the middle name. like if there is two folder
"This.is.a.Test.Folder"
"Test.this.is.a.Folder"
I want powershell copy only "Test.this.is.a.Folder"
any help please
$dest = "D:\2";
$include= #("*Test*")
Get-ChildItem $source -recurse -Force -Verbose -include $include | copy-Item -Destination {Join-Path $dest $_.FullName.Substring($source.length)}```
Your wildcard is meant to capture anything that contains the word Test in this case.
If you want to specifically start with the word Test followed by anything: Test*
Contrary, anything that ends with the word Test would be: *Test
$include = #( "Test*" )
Get-ChildItem $source -Include $include -Recurse -Force -Verbose |
Copy-Item -Destination {
Join-Path $dest -ChildPath $_.FullName.Substring($source.length)
}
Note, that you can use -File to filter only files and -Directory to filter only folders.

How to use PowerShell Get-ChildItem to retrieve files AND folders?

I'm using the Get-ChildItem command in a script. I just noticed that it will return file names beginning with bernie3_first or bernie3_second, but not folders. How can this be modified to return folders as well?
$FileNames = Get-ChildItem -Path $FilePath -Include bernie3_first*,bernie3_second* -File -Recurse | select BaseName
Your code shows a parameter that is filtering the results to only show the files and not folders. Filter parameter is -File.
Here an example:
# This would get content of C:\Test, files and folders
Get-ChildItem -Path C:\Test
# This would get content of C:\Test, only folders
Get-ChildItem -Path C:\Test -Directory
# This would get content of C:\Test, only files
Get-ChildItem -Path C:\Test -File
If you want to read more about each of the parameters you can check this on the documentation.

Powershell Loop through folder move file, create directory if doesn't exist

Good Afternoon,
I apologize if this is a basic question, but I have been struggling with it, also still very new to Powershell.
I have a network mapped folder Z:\Test.
Under Z:\Test is multiple subfolders with the same structure. I need to loop through all of the subfolders and move all PDF files if they exist in a specific location.
Z:\Test\1\Work\PDF\*.PDF - then move
Z:\Test\2\Work\PDF\*.PDF - Move So on and so on.
I have tried the following, but like I said I have been struggling with it. Thanks any help
Get-ChildItem -Path Z:\temp\*\Work -File -Include "*.PDF" -Recurse | Copy-Item -Force -Destination Y:\Temp\*\Work
I would try something like this:
$files = Get-ChildItem -Path Z:\Temp\*\Work -File -Include "*.PDF" -Recurse
foreach ($file in $files) {
Copy-Item -Path $file.FullName -Destination Y:\Temp\*\Work -Force
}

Powershell archive multiple directories in single zip archive

UPD1 - I reworked my question to make it less vague
I'm new in Powershell and need someone advise.
I have a directories, which I need to zip, for example:
in C:\inetpub\wwwroot\STAGETEST\
App_Config
Resources
bin
There are another bunch of directories in C:\inetpub\wwwroot\STAGETEST\ and I need to zip only App_Config, Resources, bin - with all subdirectories and files, keeping structure.
For this I have a script:
$SOURCEDIR = "C:\inetpub\wwwroot\STAGETEST\"
$SOURCEFOLDERS = "App_Config", "Resources", "bin"
Get-ChildItem -Path $SOURCEDIR -Include "$SOURCEFOLDERS" -Directory
$SOURCE = Get-ChildItem -Path $SOURCEDIR -Directory
Add-Type -assembly "system.io.compression.filesystem"
Foreach ($s in $SOURCE)
{
$DESTINATIONDIR = Join-path -path $SOURCEDIR -ChildPath "$($s.name).zip"
#Check if archive already exists and delete it
If(Test-path $DESTINATIONDIR) {Remove-item $DESTINATIONDIR}
[io.compression.zipfile]::CreateFromDirectory($s.fullname, $DESTINATIONDIR)
Unfortunately, I cannot understand, how to implement proper Get-ChildItem -include to get all necessary multiple directories?
Perhaps, is there any other approach?
according to Get-ChildItem help:
Include -
Specifies, as a string array, an item or items that this cmdlet includes in the operation. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as *.txt. Wildcards are permitted.
The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a
directory, such as C:\Windows*, where the wildcard character specifies the contents of the C:\Windows directory.
so, You need an array and -Recurse parameter
$SOURCEFOLDERS = #("App_Config", "Resources", "bin")
$SOURCE = Get-ChildItem -Path $SOURCEDIR -Include $SOURCEFOLDERS -Directory -Recurse

Using Remove-Item cmdlet but excluding sub-directory

I want to remove the following files from the source, however in the source there is a sub-directory that contains files with similar names. When I run the following command it is deleting files in the sub-directory with similar file name. Is there a way to just delete the files from the source and not the sub-directory?
Example: test_1_file, test_2_file, test_3_file exists in each directory, TestFolder and TestFolder/sub
$source = testfolder
remove-item -Path $source -filter test_*_file -recurse -force
It's usually easiest to pipe the output of Get-ChildItem cmdlet into Remove-Item. You then can use the better filtering of Get-ChildItem as I think -Recurse in Remove-Item has some issues. You can even use Where-Object to further filter before passing to Remove-Item
$source = testfolder
Get-ChildItem -Path $source -Filter test_*_file -Recurse |
Where-Object {$_.Fullname -notlike "$source\sub\*"} |
Remove-Item -Force
If the files to delete:
are all located directly in $source
and no other files / directories must be deleted:
Remove-Item -Path $source/test_*_file -Force
No need for -Recurse (as #Bill_Stewart notes).
Note: For conceptual clarity I've appended the wildcard pattern (test_*_file) directly to the $source path.
Using a wildcard expression separately with -Filter is generally faster (probably won't matter here), but it has its quirks and pitfalls.