foreach

Example

Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}

$List = Import-Csv (Get-FileName)

Foreach ($Item in $List) {
    Write-Output "Restricting mailflow for user" $item.emailaddress
    Set-Mailbox -Identity $item.emailaddress -RequireSenderAuthenticationEnabled $true
    Set-Mailbox -Identity $Item.emailaddress -AcceptMessagesOnlyFrom "PostMaster@lmwindpower.com"
    }

Example 2

$List = Import-Csv (Get-FileName)

$OfficeDetails = Foreach ($Item in $List) {
    Get-AdUser -Identity $item.emailaddress -Properties * | Select SamAccountName,Office
    }
    $OfficeDetails | Export-Csv f:\ngr\output.csv -NoTypeInformation

Example 3

$username = "ngr"
$users = ("$username","$username-admins","$username-adminc","$usernameamind","$username-adminz")
Foreach ($User in $users){Echo $User}'
Last modified July 21, 2024: update (e2ae86c)