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.
Related
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
I need to create an archive file of the files in C:\src\csv without recursion. It appears that Compress-Archive will always recurse subdirectories.
The following code fails. It appears to only be using the file Name and not FullName in the list of files. In what ways could I overcome this?
Compress-Archive -Path (Get-ChildItem -File -Path 'C:\src\csv') -DestinationPath $Env:TEMP\t.zip
Specify files FullName and indicate an array of values with #() construct.
Compress-Archive -Path #((Get-ChildItem -File -Path 'C:\src\csv').FullName) -DestinationPath $Env:TEMP\t.zip
I have a folder that contains several thousand files. I would like to write a Powershell script that loops through the files and copies each file whose filename contains a specific keyword. In pseudocode:
For each file in C:\[Directory]
If filename contains "Presentation" Then
copy file in C:\[Directory 2]
Simply like this ?
copy-item "C:\SourceDir\*Presentation*" "C:\DestinationDir"
or like this :
copy-item "C:\SourceDir\*" "C:\DestinationDir" -Filter "*rrrr*"
But a risk exist if you have a directory with "presentation" in his name into the source directory. Then take all method proposed here and add -file in get-childitem command.
Like in this short version of Robdy code :
gci "C:\SourceDir" -file | ? Name -like "*Presentation*" | cpi -d "C:\DestinationDir"
That code should do the trick:
$files = Get-ChildItem -Path "C:\path\to\source\folder"
$files | Where-Object Name -Like "*Presentation*" | Copy-Item -Destination "C:\path\to\destination\folder"
Of course can be written in one line but I put in two for visibility.
Edit: as Esperento57 pointed out, you might want to add -ItemType File to Get-ChildItem cmdlet to not include folders with 'Presentation' in their name. Also, depending on your needs you might also want to use -Recurse param to include files in subfolders.
If you have files in subfolders and you want to keep the path in destination folder you'll have to change the script a bit to something like:
Copy-Item -Destination $_.FullName.Replace('C:\path\to\source\folder','C:\path\to\destination\folder')
And for the above you'll have to make sure that folders are actually created (e.g. by using -Force for Copy-Item.
This seems to work:
$src = "Dir1"
$dst = "Dir2"
Get-ChildItem $src -Filter "*Presentation*" -Recurse | % {
New-Item -Path $_.FullName.Replace($src,$dst) -ItemType File -Force
Copy-Item -Path $_.FullName -Destination $_.FullName.Replace($src,$dst) -Force
}
Try something like this:
Get-ChildItem "C:\Your\Directory" -File -Filter *YourKeyWordToIsolate* |
Foreach-Object { Copy-Item $_.FullName -Destination "C:\Your\New\Directory" }
... but, of course, you'll need to fill in some of the blanks left open by your pseudocode example.
Also, that's a one-liner, but I inserted a return carriage for easier readability.
I have 4 folders in a directory. All of those folders contains some files. I want to list the file names only from the two folders.
C:\MainFolder\FolderOne\FileOne.txt
C:\MainFolder\FolderTwo\FileTwo.txt
C:\MainFolder\FolderThree\FileThree.txt
C:\MainFolder\FolderFour\FileFour.txt
I only want to list the files under FolderTwo and FolderThree.
If I use -Recurse -Include "FolderNames" it'll list only the folder names.
$source="c:\MainFolder" #location of starting directory
$files=#("*.txt", "*.doc") #if you want to include extensions add -include ($files) to get-ChildItem
Get-ChildItem -recurse ($source) -File | Where-Object {$_.PSParentPath -match "Two|Three"}
get-childitem -recurse 'foldertwo','folderthree'
Did it like this
$arrPath ='C:\MainFolder\FolderTwo','C:\MainFolder\FolderThree'
get-childitem -recurse $arrPath
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