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

Microsoft 365 and Office | SharePoint | For business | Windows
Microsoft 365 and Office | Excel | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,906 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 36,411 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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.