# clarification on $psitem or $\_

**URL:** <https://forums.powershell.org/t/clarification-on-psitem-or/15186>\
**Category:** PowerShell Help\
**Created:** [November 6, 2020, 4:05am UTC](https://forums.powershell.org/t/clarification-on-psitem-or/15186 "2020-11-06T04:05:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![garegin-asatryan](https://avatars.discourse-cdn.com/v4/letter/g/5e9695/32.png) [@garegin-asatryan](https://forums.powershell.org/u/garegin-asatryan)\
**Post date:** [November 6, 2020, 4:05am UTC](https://forums.powershell.org/t/clarification-on-psitem-or/15186/1 "2020-11-06T04:05:28Z")

</div>

I have kind of a dumb question regarding $psitem. Why is it that even when the pipeline output is a single object, I have to use a for loop to get $psitem working. for example

`Get-CimInstance Win32_OperatingSystem |% {$.LocalDateTime-$.LastBootUpTime}`

works, but

`Get-CimInstance Win32_OperatingSystem | {$.LocalDateTime-$.LastBootUpTime}`

gives an error

“Expressions are only allowed as the first element of a pipeline.”

&nbsp;

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [November 6, 2020, 4:17am UTC](https://forums.powershell.org/t/clarification-on-psitem-or/15186/2 "2020-11-06T04:17:34Z")

</div>

Not a dumb question, you are learning. {} is script block when you call it like that, which you cannot pipe to. To create values based on arithmetic, you should use a calculated expression:

```
Get-CimInstance Win32_OperatingSystem | Select-Object -Property *, @{Name='MyValue';Expression={$_.LocalDateTime-$_.LastBootUpTime}}
```

While that gets you a value, normally you ware looking for minutes or days, so you would use New-TimeSpan to get the days and round the value:

```
Get-CimInstance Win32_OperatingSystem | Select-Object -Property *, @{Name='MyValue';Expression={[math]::Round((New-TimeSpan -Start $_.LastBootUpTime -End $_.LocalDateTime).TotalDays,2)}}
```

Also, $\_ and $PSItem are tokens in the context of a row or line. Powershell uses PSObjects, which from a simple explanation is an array of hashtable. Now it’s more complex than that, but for a simple visual demonstration:

```
#Empty array
$newObject = @()

#Add a hashtable to the array
$newObject += [PSCustomObject]@{
    FirstName = 'John'
    LastName = 'Smith'
}

#Add a hashtable to the array
$newObject += [PSCustomObject]@{
    FirstName = 'Sally'
    LastName = 'Wallace'
}

$newObject
```

Even if you processing a single item like you are getting from WMI in this case, it still looks like this:

```
@( #array
    @{#hash
        FirstName = 'Sally'
        LastName = 'Wallace'
    }
)
```

There is still an array to process, so you still need to process foreach item in the array. The calculated expression $\_ is still referencing the context of the row. This is trying to keep the explanation as simple as possible, but hopefully this explains the structure and why you need the foreach.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:29pm UTC](https://forums.powershell.org/t/clarification-on-psitem-or/15186/3 "2024-05-16T20:29:10Z")

</div>

Closed on May 16, 2024, 8:29pm UTC


