Vinodh Elumalai
Vinodh Elumalai

Reputation: 77

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]

Upvotes: 1

Views: 3780

Answers (2)

Vinodh Elumalai
Vinodh Elumalai

Reputation: 77

$scriptpath='c:\temp'
$fileNames = Get-ChildItem -File -Path $scriptPath -Recurse -Include *.zip | Select -ExpandProperty Name

foreach ($vinodh in $fileNames)
{
write-host $vinodh

}

Upvotes: -1

Drew
Drew

Reputation: 4020

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

Upvotes: 4

Related Questions