Run script on multiple users (using Excel table)

Emiliano 20 Reputation points

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 "" needs to be removed from those 2 OneDrive Sites.

User's image

$User = ""

$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.


A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
1,620 questions
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,076 questions
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

    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 = ""
    $AdminSite = ****************************
    Connect-SPOService -URL $AdminSite -creds
    Import-Excel C:\temp\testim.xlsx -startcolumn 4 |
            Remove-SPOUser -Site $_.testi -LoginName $User

1 additional answer

Sort by: Most helpful
  1. MotoX80 32,531 Reputation points

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

    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.

    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.