1

I am trying to save the list of files in a directory to array list, can you please share the code snippet for Windows PowerShell.

I have 2 files under c:\temp

file1: stack.zip
file2: overflow.zip

Need to store file1 & file2 in a array called

$arrlst = ['stack.zip','overflow.zip'] Set-Location -Path "c:\temp" fileslst = Get-children $arrlst = [filelist] 

2 Answers 2

4

Running the below will get you what you are after.

[System.Collections.ArrayList]$arrlst = @( $(Get-ChildItem -File -Path 'C:\temp' | Select -ExpandProperty Name) ) 

You need to do Select -ExpandProperty Name to ensure that the only result from the Get-ChildItem is the filename including extension (Name).

Sign up to request clarification or add additional context in comments.

3 Comments

it partially worked, i got the output as single array value, stack.zip overflow.zip as single value, but i can customize.. thank you Drew
@VinodhElumalai Sorry about that, I have updated the answer to properly accommodate your request.
thank you @drew, i tried below one and it worked. $scriptpath='c:\temp' $fileNames = Get-ChildItem -File -Path $scriptPath -Recurse -Include *.zip | Select -ExpandProperty Name foreach ($vinodh in $fileNames) { write-host $vinodh $a,$b= $vinodh.split(').') $b[2].Substring(1) }
-1
$scriptpath='c:\temp' $fileNames = Get-ChildItem -File -Path $scriptPath -Recurse -Include *.zip | Select -ExpandProperty Name foreach ($vinodh in $fileNames) { write-host $vinodh } 

1 Comment

This doesn't meet your own requirements of creating an array list. This also took 11.230 seconds to run against 5,500 files. My answer will do what you require in 0.450 seconds for 5,500 files. The Write-Host command is what is causing the large amount of time in your script and not adding it to an array list.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.