0

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}

2

2 Answers 2

2

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

Calling the object with the attribute, fix the issue. Thanks.
$list=@{ FirstFile = $data1.FullName SecondFile = $data2.FullName }
0
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.