Search code examples
powershellget-childitem

Add list of files in a directory to array list


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]

Solution

  • 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).