Run script on multiple users (using Excel table)

Emiliano 20 Reputation points
2024-06-10T08:03:49.4266667+00:00

Hello everyone,

im trying to build a PowerShell script that runs a command multiple times but with different URLs, but i cant quite understand how to build it.

The point is to remove a user from the Share Point sites listed in the column D.

So lets say the user "max.muster@microsoft.com" needs to be removed from those 2 OneDrive Sites.

User's image

$User = "max.muster@microsoft.com"

$AdminSite = ****************************

Connect-SPOService -URL $AdminSite -creds

Import-Excel C:\temp\testim.xlsx -startcolumn 4

    Remove-SPOUser -Site $tesi -LoginName $User

Im quite new to PowerShell, but havent found the right method to optimize this procedure.

Thanks in Advance.

Emiliano

Excel
Excel
A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,620 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,076 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,238 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,431 Reputation points
    2024-06-10T15:21:22.9533333+00:00

    Import-Excel returns a series of PSCustomObjects. The properties of the objects are the names from the column headers in the worksheet.

    Give this a try:

    $User = "max.muster@microsoft.com"
    $AdminSite = ****************************
    Connect-SPOService -URL $AdminSite -creds
    
    Import-Excel C:\temp\testim.xlsx -startcolumn 4 |
        ForEach-Object{
            Remove-SPOUser -Site $_.testi -LoginName $User
        }
    

1 additional answer

Sort by: Most helpful
  1. MotoX80 32,531 Reputation points
    2024-06-10T14:01:32.4866667+00:00

    Here is a good document to refer to if you are learning Powershell.

    https://www.sapien.com/books_training/Windows-PowerShell-4

    In the statements that you posted, you have Import-Excel but you don't assign the data to any variable. How would you reference the data?

    There are plenty of examples on the internet.

    https://www.bing.com/search?q=powershell+import-excel+examples

    Review a few examples and see how others read spreadsheets.

    Build your script block by block. That is, focus on processing the spreadsheet first. Figure out how to read it and to display the contents before you try to add in additional functionality (Sharepoint calls).

    Use Powershell_ISE to test your script. It is already installed on Windows.