Created
May 30, 2024 12:05
-
-
Save r0ckbeard/526f530d87373594f6f042b3b320fad8 to your computer and use it in GitHub Desktop.
Imports users from csv to SCCM/MECM user collection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Import SCCM module | |
| Import-Module 'C:\Program Files (x86)\Microsoft Endpoint Manager\AdminConsole\bin\ConfigurationManager.psd1' | |
| # Define the Site Code | |
| $SiteCode = "" | |
| # Define the path to the CSV file | |
| $csvPath = "" | |
| # Define the collection ID to which users will be added | |
| $collectionID = "" | |
| # Import the CSV | |
| $users = Import-Csv -Path $csvPath | |
| foreach ($user in $users) { | |
| # Extract the actual username value | |
| $userName = $user.UserName.Trim() | |
| # Get the user resource ID | |
| $userResourceID = Get-CMUser -Name $userName | Select-Object -ExpandProperty ResourceID | |
| if ($userResourceID) { | |
| # Add each user to the collection | |
| Add-CMUserCollectionDirectMembershipRule -CollectionId $collectionID -ResourceID $userResourceID | |
| } else { | |
| Write-Host "User $userName not found in SCCM." | |
| } | |
| } | |
| Write-Host "Users have been added to the collection." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment