user4600330
user4600330

Reputation:

Remove the @{} tag from object output PowerShell

I need help removing the @{} extensions from object output.

The code bellow is listing the last modified file inside a folder. But the output is inside extension @{}.

I have tried the Out-String but it is not working.

function scriptA() {
Get-ChildItem $path | Where-Object {!$_.PsIsContainer} | Select fullname -last 1
}

function scriptB() {
Get-ChildItem $path2 | Where-Object {!$_.PsIsContainer} | Select fullname -last 1
}

$data1=ScritA
$data2=ScriptB

$result=@()
$list=@{
FirstFile=$data1
SecondFile=$data2
}

$result+= New-Object psobject -Property $list
$result | Export-Csv -append -Path  $csv

This will output: FirstFile @{data1} and SecondFile @{data2}

Upvotes: 0

Views: 2213

Answers (2)

user9952217
user9952217

Reputation:

New-Object PSObject -Property @{FirstFile = ($a = (Get-ChildItem $path1),(Get-ChildItem $path2) |
Where-Object {!$_.PSIsContainer} | ForEach-Object {$_[-1].FullName})[0];SecondFile = $a[1]} |
Export-Csv $csv -NoTypeInformation

Upvotes: 0

Vivek Kumar Singh
Vivek Kumar Singh

Reputation: 3350

Change your functions slightly to this -

function scriptA() {
Get-ChildItem $path | Where-Object {!$_.PsIsContainer} | Select-Object -ExpandProperty fullname -last 1
}

function scriptB() {
Get-ChildItem $path2 | Where-Object {!$_.PsIsContainer} | Select-Object -ExpandProperty fullname -last 1
}

Doing that will let you select only the FullName property.

OR

If you do not want to change the functions, change the $list assignment to -

$list=@{
FirstFile = $data1.FullName
SecondFile = $data2.FullName
}

Upvotes: 2

Related Questions