简体   繁体   中英

SharePoint > Powershell > How to Get person field's Display Name?

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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM