# Getting a substring from a Select-Object / Get-Aduser command

**URL:** <https://forums.powershell.org/t/getting-a-substring-from-a-select-object-get-aduser-command/20764>\
**Category:** PowerShell Help\
**Created:** [October 28, 2022, 10:21am UTC](https://forums.powershell.org/t/getting-a-substring-from-a-select-object-get-aduser-command/20764 "2022-10-28T10:21:29Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Liam\_R](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Liam\_R](https://forums.powershell.org/u/Liam_R)\
**Post date:** [October 28, 2022, 10:21am UTC](https://forums.powershell.org/t/getting-a-substring-from-a-select-object-get-aduser-command/20764/1 "2022-10-28T10:21:29Z")

</div>

In the code below, the userprinciplename will output strings like “[LLL\_John.Smith@email.com](mailto:LLL_John.Smith@email.com)” and [XXXX\_Jane.Doe@email.com](mailto:XXXX_Jane.Doe@email.com)" but i am hoping to extract a substring from that, that being the codes before the “\_” character, so LLL and XXXX.

There may be some which do not have an underscore character however and so these would need to be ignored / have the original string it would have returned.

```auto
##Check bottom of script for setting specific OU
Function Get-LastLogon {
  param(
    [string]$OUName
  )

  # Get all matching OUs on any level
  $OUs = Get-ADOrganizationalUnit -Filter "Name -like '$OUName'"
  $DCs = Get-ADDomainController -Filter *

  # Get all users from each OU from each DC
  $ADUsers = Foreach ($OU in $OUs) {
    Foreach ($DC in $DCs.HostName) {
      Get-ADUser -SearchBase $OU.DistinguishedName -Filter * -Properties LastLogon -server $dc | 
        Select-Object Name,userPrincipalName, @{n='LastLogon';e={[DateTime]::FromFileTime($_.LastLogon)}}
    }
  }

  # return most recent LastLogon date for each user
  $ADUsers | 
    Group Name,userPrincipalName | 
    Select Name,userprinciplename, @{n='LastLogon';e={$_.Group.LastLogon | sort -desc | select -First 1}}
} ## End function

$OUcustom = Read-Host -prompt 'Enter OU here or "Clients" for all'
##Enter the OU here
Get-LastLogon -OUName $OUcustom |
##export csv
Export-Csv -path "C:\temp\UserExport_$((Get-Date).ToString("ddMM_HHmm")).csv" -NoTypeInformation
".csv extracted to C:\temp"
pause

```

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 28, 2022, 10:49am UTC](https://forums.powershell.org/t/getting-a-substring-from-a-select-object-get-aduser-command/20764/2 "2022-10-28T10:49:30Z")

</div>

Hi, welcome to the forum 👋

You can just use `-split` for this. If it doesn’t split, you’ll get the original string.

```auto
PS E:\Temp> ('LLL_John.Smith@email.com' -split '_')[0]
LLL
PS E:\Temp> ('LLLJohn.Smith@email.com' -split '_')[0]
LLLJohn.Smith@email.com

```

---

<div class="post-metadata">

**Author:** ![Liam\_R](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Liam\_R](https://forums.powershell.org/u/Liam_R)\
**Post date:** [October 28, 2022, 11:03am UTC](https://forums.powershell.org/t/getting-a-substring-from-a-select-object-get-aduser-command/20764/3 "2022-10-28T11:03:50Z")

</div>

This script is going to return hundreds of different variations of email, this would mean i’d need to incorporate every single one into the script no? - Apologies if i am misunderstanding.  
The email examples were just that, i need a way of basically doing the below, but in a way that works:

```auto
Select-Object userPrincipalName.Substring(0,'_')

```

It outputs all of this to a CSV and whilst i could truncate everything in the userPrincipalName column before the underscore in excel for the result i want, it’d be nice for this to be done in the PS script without any input needed from the person running the script.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 28, 2022, 11:15am UTC](https://forums.powershell.org/t/getting-a-substring-from-a-select-object-get-aduser-command/20764/4 "2022-10-28T11:15:49Z")

</div>

I understand that, it was just an example to show the output.

You can either assign `userPrinicalName` to a variable and split the variable, or do it as a calculated property in your `Select-Object` command.

---

<div class="post-metadata">

**Author:** ![Liam\_R](https://avatars.discourse-cdn.com/v4/letter/l/a6a055/32.png) [@Liam\_R](https://forums.powershell.org/u/Liam_R)\
**Post date:** [October 28, 2022, 11:26am UTC](https://forums.powershell.org/t/getting-a-substring-from-a-select-object-get-aduser-command/20764/5 "2022-10-28T11:26:07Z")

</div>

That’s great, just got it working, thanks!

---

<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:16pm UTC](https://forums.powershell.org/t/getting-a-substring-from-a-select-object-get-aduser-command/20764/6 "2024-05-16T20:16:27Z")

</div>

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


