locked
Powershell > How to get person field display name RRS feed

  • Question

  • I am running the following ps script to get some data out to csv. The person fields (AssignedTo and CreatedBy) come with index;#LastName, FirstName. How do just get the name without the index stuff. Or do I need to do a Regex or Replace? For example,

    32;#Doe, John Should be Doe, John

    $results = @()
    $web = Get-SPWeb "http://ourlocal.company.cc/docs/sales"
    $list = $web.Lists["Sales Tasks"]
    $caml = '<Where><Eq><FieldRef Name="Status" /><Value Type="Choice">Completed</Value></Eq></Where>'
    $query=new-object Microsoft.SharePoint.SPQuery
    $query.Query=$caml
    $ListItems=$list.GetItems($query)
    Write-Host "count " $ListItems.Count
    foreach ($item in $listItems)
    {
             $Title = $item["Title"]
             $AssignedTo = $item["AssignedTo"]
             $Status = $item["Status"]
             $Priority = $item["Priority"]
             $CreatedBy = $item["Created By"]
    
             $out = new-object psobject -Property @{Title = $Title
                           AssignedTo = $AssignedTo
                           Status = $Status
                           Priority = $Priority
                           "Due Date" = $DueDate
                           "Percent Complete" = $Percent
                           "Created by" = $CreatedBy}
    
    
            $out = $out|select-object Title, AssignedTo, Status, Priority, "Due Date", "Percent Complete", "Created by"
            $results +=$out
        }
    $results | Export-Csv "c:\output.csv" -noType
    $web.Dispose()


    Thursday, August 28, 2014 12:39 PM

Answers

  • Step 1:

    Add following code right after $CreatedBy=$item["Created By"]

    $CreatedByUserObj = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $CreatedBy)

    $CreatedByDisplayName = $CreatedByUserObj.User.DisplayName;

    Step 2:

    Replace "Created by" = $CreatedBy with following code

    "Created by" = $CreatedByDisplayName


    Thursday, August 28, 2014 12:52 PM

All replies

  • Step 1:

    Add following code right after $CreatedBy=$item["Created By"]

    $CreatedByUserObj = New-Object Microsoft.SharePoint.SPFieldUserValue($web, $CreatedBy)

    $CreatedByDisplayName = $CreatedByUserObj.User.DisplayName;

    Step 2:

    Replace "Created by" = $CreatedBy with following code

    "Created by" = $CreatedByDisplayName


    Thursday, August 28, 2014 12:52 PM
  • Thanks Moe for the quick reply. It worked. You ROCK !!!!!!!!! :)
    Thursday, August 28, 2014 1:04 PM