List all user profile properties with PowerShell - PowerShell query

5,624

I've further developed the code so that it now accepts a comma delimited list of properties and writes them to a delimited file.

# Outputs a delimited file with specified user profile properties for each user in Sharepoint

# Create array of desired properties
$arProperties = 'UserName','FirstName','LastName','Title','WorkEmail','WorkPhone','Manager','AlternateContact','RoleDescription','PictureURL';
# Specify output file
$outfile = 'UserProfiles.csv';
#Specify delimiter character (i.e. not one that might appear in your user profile data)
$delim = '^';
# Specify Shared Service Provider that contains the user profiles.
$SSP = "SharedServices";

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")

# Function:          Get-UserProfiles
# Description:       return a UserProfileManager object containing all user profiles
# Parameters:        SSPName          SSPName    
#
Function global:Get-UserProfiles($SSPName)
{
 $ServerContext = [Microsoft.Office.Server.ServerContext]::GetContext($SSPName);
 $UPManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServerContext);
 return $UPManager.GetEnumerator();
}
$profiles = Get-UserProfiles($SSP);

#Initialise Output file with headings
$header = [string]::join($delim,$arProperties);
Write-Output $header | Out-File $outfile

#Output the specified properties for each
$profiles | ForEach-Object {
 foreach($p in $arProperties){
  # Get the property name and add it to a new array, which will be used to construct the result string
  $arProfileProps += $_.Item($p);
 }  
 $results = [string]::join($delim,$arProfileProps);
 # Get rid of any newlines that may be in there.
 $CleanResults = $results.Replace("`n",'');
 Write-Output $CleanResults
 Remove-Variable -Name arProfileProps
} | Out-File -Append $outfile

This gets me a bit closer. I'd still really like a script that iterates through all the profile properties and puts them into a CSV or XML file more gracefully. This will do for now.

Share:
5,624

Related videos on Youtube

dunxd
Author by

dunxd

I'm currently freelance specialising in international connectivity and infrastructure working with clients in the humanitarian space. If your organisation struggles to work effectively because of limited internet options in far flung locations, maybe I can help. Until 2017 I worked at a large international development charity in London, as International Operations Manager. I managed a team of Regional ICT Service Managers, based in developing world countries, who kept the users happy through fixing problems, setting up great connectivity and generally making sure users could do their day jobs. I think I did a good job as a manager - some of my team went on to great things! I previously worked at the same place as International Network Systems Analyst. I looked after a bunch of ICT systems in offices in the developing world, as well as looking after systems in our HQ. I gained a lot of knowledge in that job, and the techy side competes with the people stuff in the new role, hence I still hang out here a lot. I'm passionate about the use of ICT in developing countries, both in terms of dealing with the inherent problems for ICT in those places, and using ICT as a tool for development.

Updated on September 17, 2022

Comments

  • dunxd
    dunxd over 1 year

    The following script spits out all UserProfile properties for users on Sharepoint 2007:

    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server")
    [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.UserProfiles")
    # Function:          Get-UserProfiles
    # Description:       return a UserProfileManager object containing all user profiles
    # Parameters:        SSPName          SSPName    
    #
    Function global:Get-UserProfiles($SSPName)
    {
        $ServerContext = [Microsoft.Office.Server.ServerContext]::GetContext($SSPName);
        $UPManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServerContext);
        return $UPManager.GetEnumerator();
    }
    
    $profiles = Get-UserProfiles("SharedServices");
    $profiles | ForEach-Object { $_.GetEnumerator();}
    

    However, what I want to do is be able to return a table, or csv file of all in the profile,

    So far I am only able to get specific properties (see answer below). I have tried piping the output to |ft and | select but this just returns blanks.

    I feel like I am so close. I don't want to replace the $_.GetEnumerator() call with lots of $_.Item("property") calls and it doesn't feel like I should have to. Any ideas?