# Chaging existing and adding new properties in a single pass

**URL:** <https://forums.powershell.org/t/chaging-existing-and-adding-new-properties-in-a-single-pass/11228>\
**Category:** PowerShell Help\
**Created:** [August 29, 2018, 11:17pm UTC](https://forums.powershell.org/t/chaging-existing-and-adding-new-properties-in-a-single-pass/11228 "2018-08-29T23:17:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtan2010](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rtan2010/32/1173_2.png) [@rtan2010](https://forums.powershell.org/u/rtan2010)\
**Post date:** [August 29, 2018, 11:17pm UTC](https://forums.powershell.org/t/chaging-existing-and-adding-new-properties-in-a-single-pass/11228/1 "2018-08-29T23:17:45Z")

</div>

I have a CSV file as follows:

```
"Title","Date","Comments"
"aaa","1/23/2016","action"
"bbb","8/31/2017","drama"
"ccc","12/4/2017","western"
"ddd","7/11/2018","thriller"
```

I have the following PS script:

```
$data = Import-csv .\Input.csv

$data | Select-object -property * |
    Foreach-object -process {
    @{ Newdate = ($_.Date -as [Datetime]).AddYears(2).ToString()); } |
Out-gridview
```

This works fine, but:

Q1 - how can I format the ToString() result to show only Date without the trailing TIME?

Q2 - is it possible to add a new property (towards the ultimate output file, not shown in the sample code) without an extra pipe to Add-Member?

Would be grateful for any tips or hints.

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [August 30, 2018, 12:32am UTC](https://forums.powershell.org/t/chaging-existing-and-adding-new-properties-in-a-single-pass/11228/2 "2018-08-30T00:32:05Z")

</div>

**ForEach-Object** is not required here, you can do it via calculated properties.

```
$data | Select-object -property Title,@{E={($_.Date -as [Datetime]).AddYears(2).ToString()};L='Date'},comments
```

Below is a good article by Adam on this.

> **[Using PowerShell's Calculated Properties -- Microsoft Certified Professional...](https://mcpmag.com/articles/2017/01/19/using-powershell-calculated-properties.aspx)**
>
> This guide will walk you through changing property names and values for the Select-Object cmdlet.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [August 30, 2018, 12:55am UTC](https://forums.powershell.org/t/chaging-existing-and-adding-new-properties-in-a-single-pass/11228/3 "2018-08-30T00:55:09Z")

</div>

For me it works like this:

```
$Source = @’
“Title”,“Date”,“Comments”
“aaa”,“1/23/2016”,“action”
“bbb”,“8/31/2017”,“drama”
“ccc”,“12/4/2017”,“western”
“ddd”,“7/11/2018”,“thriller”
'@

$Data = ConvertFrom-Csv -Delimiter ‘,’ -InputObject $Source

$data |  
Foreach-object -process {  
[PSCustomObject]@{  
Title = $_.Title  
Date = (Get-Date -Year ($_.Date -split ‘/’)[2] -Month ($_.Date -split ‘/’)[0] -Day ($_.Date -split ‘/’)[1]).Date.AddYears(2)  
Comments = $\_.Comments  
}  
}|  
Out-gridview
```

---

<div class="post-metadata">

**Author:** ![rtan2010](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rtan2010/32/1173_2.png) [@rtan2010](https://forums.powershell.org/u/rtan2010)\
**Post date:** [August 30, 2018, 3:36am UTC](https://forums.powershell.org/t/chaging-existing-and-adding-new-properties-in-a-single-pass/11228/4 "2018-08-30T03:36:49Z")

</div>

Thanks very much Mr KVPrasoon, I had just amended my question - to a slightly different one, since I already found my mistake.

---

<div class="post-metadata">

**Author:** ![rtan2010](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rtan2010/32/1173_2.png) [@rtan2010](https://forums.powershell.org/u/rtan2010)\
**Post date:** [August 30, 2018, 8:01am UTC](https://forums.powershell.org/t/chaging-existing-and-adding-new-properties-in-a-single-pass/11228/5 "2018-08-30T08:01:21Z")

</div>

Many thanks Mr Olaf Soyk, much appreciated.

&nbsp;

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [August 30, 2018, 8:29am UTC](https://forums.powershell.org/t/chaging-existing-and-adding-new-properties-in-a-single-pass/11228/6 "2018-08-30T08:29:06Z")

</div>

Q1 – how can I format the ToString() result to show only Date without the trailing TIME?  
Yes - you can use the ToShortDateString() method.

```
$data | Select-object -property Title,@{E={($_.Date -as [Datetime]).AddYears(2).ToShortDateString()};L='Date'},comments
```

Q2 – is it possible to add a new property (towards the ultimate output file, not shown in the sample code) without an extra pipe to Add-Member?  
Yes, same calculated properties will do that.

```
$data | Select-object -property Title,@{E={($_.Date -as [Datetime]).AddYears(2).ToShortDateString()};L='Date'},comments,@{E={$_.Title + $_.Comments};L='NewItem'}
```

---

<div class="post-metadata">

**Author:** ![rtan2010](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rtan2010/32/1173_2.png) [@rtan2010](https://forums.powershell.org/u/rtan2010)\
**Post date:** [August 30, 2018, 10:55am UTC](https://forums.powershell.org/t/chaging-existing-and-adding-new-properties-in-a-single-pass/11228/7 "2018-08-30T10:55:56Z")

</div>

Sincerest thanks Mr Kvprasoon, this is very informative and useful.

&nbsp;

---

<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:34pm UTC](https://forums.powershell.org/t/chaging-existing-and-adding-new-properties-in-a-single-pass/11228/8 "2024-05-16T20:34:30Z")

</div>


