# Triming

**URL:** <https://forums.powershell.org/t/triming/6298>\
**Category:** PowerShell Help\
**Created:** [April 26, 2016, 12:00am UTC](https://forums.powershell.org/t/triming/6298 "2016-04-26T00:00:20Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [April 26, 2016, 12:00am UTC](https://forums.powershell.org/t/triming/6298/1 "2016-04-26T00:00:20Z")

</div>

Hi All , having a day where my head just cant look at code with sense

i have a csv file that contains a bunch of names, i want to capture the first 20 characters only

```
$Users = Import-Csv "h:\usersCAL.csv" |Out-String

$string1 = foreach ($user in $users) {$user.Substring(0,10)}
```

have a feeling im way off

---

<div class="post-metadata">

**Author:** ![anthony-stringer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/anthony-stringer/32/593_2.png) [@anthony-stringer](https://forums.powershell.org/u/anthony-stringer)\
**Post date:** [April 26, 2016, 12:20am UTC](https://forums.powershell.org/t/triming/6298/2 "2016-04-26T00:20:31Z")

</div>

i think you may just need to remove |Out-String

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [April 26, 2016, 12:27am UTC](https://forums.powershell.org/t/triming/6298/3 "2016-04-26T00:27:31Z")

</div>

Remove Out-String, because it’s turning your structured data into a flat string. And, keep in mind that the way you’ve written this, $string1 is going to contain a collection of strings. Make sure that’s what you want.

---

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [April 26, 2016, 12:35am UTC](https://forums.powershell.org/t/triming/6298/4 "2016-04-26T00:35:16Z")

</div>

thanks both removing out-string give the below error  
Method invocation failed because [System.Management.Automation.PSCustomObject] does not contain a method named ‘Substring’.  
At line:3 char:39

- $string1 = foreach ($user in $users) {$user.Substring(0,10)}
- 

```
                                  ~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : InvalidOperation: (Substring:String) , RuntimeException
  - FullyQualifiedErrorId : MethodNotFound

what i am after is , passing names to an exchange command full context below (looks at samaacount name so hence 20 letters restriction

```
$Users = Import-Csv "h:\usersCAL.csv"
foreach ($User in $Users)
{
$user.Substring(0,20)
    
    add-MailboxFolderPermission -Identity Climate-ChangeEvents:\Calendar -user $User.users -AccessRights "editor"
}
```

Csv data looks like

Users  
Gemma.Andres  
Bethan.Lawson  
Michael.Baker  
Lokeshwar.Vohra  
Marialina.Klokidi  
Ewan.Frost-Pennington  
Daniel.Adegbie  
Yke.Wynia  
Laura.Hill  
Zach.Wilcox  
Aude.Lucien  
Charlotte.Graves  
Annie.Gibbons  
Lewis.Fox-James  
Paul.Garbett

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [April 26, 2016, 12:42am UTC](https://forums.powershell.org/t/triming/6298/5 "2016-04-26T00:42:45Z")

</div>

It looks like you are using a column header (Users). That is why you are getting the error. You need to take one more step and work with type: string not pscustomobject.

```
foreach ($user in $users) {
    If ($user.users.length -ge 20){
    $username = $user.users.Substring(0,20)
    Add-MailboxFolder... -User $username} Else {
    Add-MailboxFolder... -User $user.users
}}
```

---

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [April 26, 2016, 12:56am UTC](https://forums.powershell.org/t/triming/6298/6 "2016-04-26T00:56:02Z")

</div>

Hi Random

i get this when adding that extra part

```
$Users = Import-Csv "h:\usersCAL.csv"
foreach ($User in $Users)
{
$user.users.Substring(0,20)
    
   add-MailboxFolderPermission -Identity Climate-ChangeEvents:\Calendar -user $User.users -AccessRights "editor"
}
```

Exception calling “Substring” with “2” argument(s): “Index and length must refer to a location within the string.  
Parameter name: length”  
At H:\EAA\Powers Shell Commands\For Each Example (CSV)Multiple Calendar Perms.ps1:4 char:1

- $user.users.Substring(0,20)
- 

```auto
  + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
  + FullyQualifiedErrorId : ArgumentOutOfRangeException
```

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [April 26, 2016, 1:07am UTC](https://forums.powershell.org/t/triming/6298/7 "2016-04-26T01:07:59Z")

</div>

I modified my previous comment. One of your users’ length is longer than 20 characters (Ewan.Frost-Pennington : 21). It will generate an error because of that. You can add an If statement to check user length.

---

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [April 26, 2016, 1:17am UTC](https://forums.powershell.org/t/triming/6298/8 "2016-04-26T01:17:51Z")

</div>

yeah the idea is to ignore everything past the 20 character mark changeing:

Ewan.Frost-Pennington  
to  
Ewan.Frost-Penningto

hmm even just running this part i get errors,

```
$Users = Import-Csv "h:\usersCAL.csv"
foreach ($user in $users) {
    If ($user.users.length -le 20){
    $user.users.Substring(0,20)}}
```

Exception calling “Substring” with “2” argument(s): “Index and length must refer to a location within the string.  
Parameter name: length”  
At line:4 char:5

- 

```
$user.users.Substring(0,20)}}

```

- 

```
~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : NotSpecified: (🙂 , MethodInvocationException
  - FullyQualifiedErrorId : ArgumentOutOfRangeException

---

<div class="post-metadata">

**Author:** ![random-commandline](https://avatars.discourse-cdn.com/v4/letter/r/e56c9b/32.png) [@random-commandline](https://forums.powershell.org/u/random-commandline)\
**Post date:** [April 26, 2016, 1:46am UTC](https://forums.powershell.org/t/triming/6298/9 "2016-04-26T01:46:19Z")

</div>

Sorry about that, I was confused. Try my previous comment again.

---

<div class="post-metadata">

**Author:** ![bobmccoy](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/bobmccoy/32/650_2.png) [@bobmccoy](https://forums.powershell.org/u/bobmccoy)\
**Post date:** [April 26, 2016, 3:36am UTC](https://forums.powershell.org/t/triming/6298/10 "2016-04-26T03:36:20Z")

</div>

You only want to trim the string if it is -gt 20, not -le.

---

<div class="post-metadata">

**Author:** ![mark-prior](https://avatars.discourse-cdn.com/v4/letter/m/c67d28/32.png) [@mark-prior](https://forums.powershell.org/u/mark-prior)\
**Post date:** [April 26, 2016, 7:23pm UTC](https://forums.powershell.org/t/triming/6298/11 "2016-04-26T19:23:48Z")

</div>

Thanks all have this working how i need now

---

<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:41pm UTC](https://forums.powershell.org/t/triming/6298/12 "2024-05-16T20:41:31Z")

</div>


