0

The following working as expected except the format. It appears the record of value $r and $s variable are display in the same column field. How i can make display in different column field? or when i open the csv document with Excel. Thank you

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) { 
        Add-PSSnapin "Microsoft.SharePoint.PowerShell" 
} 

$timestamp = $(get-date -f MM-dd-yyyy_HH_mm_ss)
$Output=$timestamp + "_" + "SAPRoles.csv";


$wa = Get-SPWebApplication;
$site =  Get-SPSite $wa.Url;

$serviceContext = Get-SPServiceContext -Site $site

#Instantiate User Profile Manager
$userProfileConfigManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($serviceContext);

#Get All User Profiles
$profiles = $userProfileConfigManager.GetEnumerator();

#Loop through all user profiles and display account name and role
foreach($profile in $profiles)
{
 $s= $profile.get_Item("AccountName") 
 $r= $profile.get_Item("DataRoles") 
 $test= $s + $r | Out-File -Encoding Default -Append -FilePath $Output;
}

2 Answers 2

1

I think you can try something like this:

$profiles|Select-Object -Property @{Name="AccountName"; Expression = {$_.get_Item("AccountName")}},@{Name="DataRoles"; Expression = {$_.get_Item("DataRoles") -join ','}}| Export-Csv $Output -encoding "unicode"
5
  • still displaying in one column Commented Oct 1, 2014 at 4:55
  • Sorry, what do you mean by "displaying in one column" - displaying in one column in Excel? Commented Oct 1, 2014 at 5:59
  • yes, that is correct. I want it display in different column when open with Excel.Sorry about that i should state on my question.thx Commented Oct 1, 2014 at 7:10
  • So the actual problem is that delimiter symbol in Excel depends on your locale. The best way - import Text Data and set the delimiter in it's wizard. hesa.ac.uk/submit-import Commented Oct 1, 2014 at 7:24
  • hi Ruslan, I make a minor change to your answer changing -encoding "unicode" to Default is doing what i want instead of import text data method. $profiles|Select-Object -Property @{Name="AccountName"; Expression = {$_.get_Item("AccountName")}},@{Name="DataRoles"; Expression = {$_.get_Item("DataRoles") -join ','}}| Export-Csv $Output -encoding Default. Thanks Commented Oct 1, 2014 at 7:36
0

You need to output a seperator for your CSV (like ';'). It's easier if you just add your column values to an array (@() in PS) and then do something like this:

[String]::Join(";", @("columnValue1", "columnValue2"))

output:

columnValue1;columnValue2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.