Powershell Foreach multiple RRS feed

  • Question

  • I am trying to pull AD data from our server and export it to an excel file. We have multiple sites and I am trying to pull each site to its own worksheet in an excel workbook. I have this so far but it currently just creates a sheet for each site and puts all sites in each worksheet.  What am I doing wrong here? Exporting to Excel is working just the foreach is not working individually.

    $sites = (Get-ADOrganizationalUnit -Filter 'Name -like "*_Users"').DistinguishedName 
    $siteNames = (Get-ADOrganizationalUnit -Filter 'Name -like "*_Users"').Name 
    Foreach ($sitename in $sitenames) {
        Foreach ($site in $sites) {
    Get-ADUser -Filter * -SearchBase "$site" ` -Properties sAMAccountName,HomeDirectory,manager | Select sAMAccountName,HomeDirectory,Manager | Export-Excel -workSheetName "$siteName" -path  "C:\Temp\FSMigration\FSmigrationtest.xlsx"} }

    • Edited by Bjoshua Tuesday, March 12, 2019 6:37 PM
    Tuesday, March 12, 2019 6:36 PM

All replies