# encoding: ascii
# api: powershell
# title: SharePoint UserID grab
# description: Use the MOSS (Microsoft Office SharePoint Server) .NET Libraries to find SharePoint ID of known user.
# version: 0.1
# author: Craig Pilkenton
# license: CC0
# x-poshcode-id: 740
# x-archived: 2008-12-21T05:41:19
#
# Overloaded with params so that if no user is passed in, a list of all users for that
# # specific Site Collection will be returned.
# Can be used for injecting specific users into the CreatedBy & ModifiedBy column when
# # migrating data between Lists, sites, or farms.
#
## .\SharePoint_Users_Read.ps1 "http://some.urlname.com/" "User Information List" ""
## .\SharePoint_Users_Read.ps1 "http://some.urlname.com/" "User Information List" "user, someone"
param(
[string] $rqurdstrPath = $(Throw "--SharePoint Core Path required."), #required parameter
[string] $rqurdstrListName = $(Throw "--SharePoint List Name required."), #required parameter
[string] $strTargetUser = "" #non-required user name for ID Return
)
Write-Host "Beginning Processing--`n"
## Global Variables ##
$strUserIDReturn = ""
write-host "rqurdstrPath: $rqurdstrPath "
write-host "rqurdstrListName: $rqurdstrListName "
## Load SharePoint .NET Libraries ##
[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$site = new-object Microsoft.SharePoint.SPSite($rqurdstrPath)
$userlist = $site.RootWeb.Lists[$rqurdstrListName]
write-host "site: $site "
write-host "userlist: $userlist`n"
# Print the ID and Title of each list item #$userlist.Items | Format-List -property ID, Title
if($strTargetUser -eq "") {
Write-Host "--Displaying all Portal Users"
foreach($strItem in $userlist.Items) {
[string]$strItem.ID + "--"+$strItem.Title
}
}
else {
write-host "--Targeting specific user to return ID"
$intUserIDReturn = -1
foreach($strItem in $userlist.Items) {
$intUserID=[int]$strItem.ID
$strUserName=$strItem.Title
if($strUserName -eq $strTargetUser) {
$intUserIDReturn = $intUserID
break;
}
}
$intUserIDReturn
}
## NOTE: don't forget to dispose your .NET objects! ##
$site.RootWeb.Dispose()
$site.Dispose()
## End Processing ##
Write-Host "`nEnd Processing--`n"