Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a string instead of an array in PowerShell?

I'm trying to do the following:

$files = Get-ChildItem c:\temp | Select-Object Name
foreach ($i in $files) {
    Write-Host "Filename is $i"
}

Sample result:

Filename is @{Name=oracle10204.rsp}
Filename is @{Name=powershell.txt}

How do I get only the following?

Filename is oracle10204.rsp
Filename is powershell.txt
like image 700
jrara Avatar asked Dec 03 '22 06:12

jrara


2 Answers

With the -Name switch you can get object names only:

 Get-ChildItem c:\temp -Name
like image 56
Shay Levy Avatar answered Dec 06 '22 19:12

Shay Levy


If you are adamant about getting your original attempt to work, try replacing

Select-Object Name

with

Select-Object -ExpandProperty Name
like image 29
OldFart Avatar answered Dec 06 '22 18:12

OldFart