SharePoint> Powershell> How to display username?

I am running the following ps script to get some data from the csv. Person fields (AssignedTo and CreatedBy) are supplied with index: #LastName, FirstName. How easy it is to get a name without an index. Or do I need to do Regex or Replace? For example,

32; #Doe John Must 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()

      

+3


source to share


1 answer


Step 1:

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

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

$CreatedByDisplayName = $CreatedByUserObj.User.DisplayName;

      



Step 2:

Replace "Created by" = $ CreatedBy with the following code

"Created by" = $CreatedByDisplayName

      

+5


source







All Articles