Reputation: 1353
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()
Upvotes: 3
Views: 10050
Reputation: 111
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
Upvotes: 6