Showing posts with label powershell. Show all posts
Showing posts with label powershell. Show all posts

Thursday, 16 November 2017

Powershell - move AD computers from cn=computers to different OUs selected by name

this powershell script will move new computers vom the default CN=computers to an OU selected by Name





import-module ActiveDirectory

$newComputers = (Get-ADComputer -SearchBase 'CN=Computers, DC=yourdomain, DC=local' -Filter *  -ErrorAction Stop | select -Expand DistinguishedName)

Foreach  ($computer in $newComputers)
    {


        if ($Computer.StartsWith("CN=ALF1") -or $Computer.StartsWith("CN=CAP1") -or $Computer.StartsWith("CN=ERK1"))
                {
                Write-Host "$Computer moved to Europe"
                Move-ADObject -Identity $Computer -TargetPath 'ou=Europe,ou=your Sub OU,DC=yourdomain,DC=local'
                }
        ElseIf ($Computer.StartsWith("CN=ATN1") -or $Computer.StartsWith("CN=CAR1"))
                {
                   Write-Host "$Computer moved to America"
                Move-ADObject -Identity $Computer -TargetPath "ou=America,ou=your Sub OU,DC=yourdomain,DC=local"
                }
        Else         {
                write-host "not matching"
                }
    }





Thursday, 21 August 2014

Powershell - get all mailboxes size from exchange server

get-mailbox -server yourexchangeservername -ResultSize unlimited | select-object Alias, IssueWarningQuota, ProhibitSendQuota, @{label="TotalItemSize(MB)";expression={(get-mailboxstatistics $_).TotalItemSize.Value.ToMB()}}, @{label="ItemCount";expression={(get-mailboxstatistics $_).ItemCount}}, Database | Export-Csv "UserMailboxSizes.csv" –NoTypeInformation

Powershell - Move Mailboxes from a text file in Exchange 2007 with Powershell


Get-Content users.txt | move-mailbox -TargetDatabase 'EXCHANGESERVERNAME\EXCH-SG-001\EXCH-MBX-001'  -Confirm:$false

Wednesday, 20 August 2014

Powershell - delete log files older than 7 days


#----- define parameters -----#
#----- get current date ----#
$Now = Get-Date
#----- define amount of days ----#
$Days = "7"
#----- define folder where files are located ----#
$TargetFolder = "F:\folder"
#----- define extension ----#
$Extension = "*.log"
#----- define LastWriteTime parameter based on $Days ---#
$LastWrite = $Now.AddDays(-$Days)

#----- get files based on lastwrite filter and specified folder ---#
$Files = Get-Childitem $TargetFolder -Include $Extension -Recurse | Where {$_.LastWriteTime -le "$LastWrite"}

foreach ($File in $Files)
    {
    if ($File -ne $NULL)
        {
        write-host "Deleting File $File" -ForegroundColor "Red"
        Remove-Item $File.FullName | out-null
        }
    else
        {
        Write-Host "No more files to delete!" -foregroundcolor "Green"
        }
    }

Powershell - get a list of all connected ActiveSync devices to your Exchange


$devices = @()
$mailboxes = Get-CASMailbox -ResultSize:Unlimited | Where-Object {$_.HasActiveSyncDevicePartnership -eq $true -and $_.ExchangeVersion.ExchangeBuild -ilike "8*"}

foreach ($m in $mailboxes)
{
    $devices += Get-ActiveSyncDeviceStatistics -Mailbox $m.Identity
}

$devices | Export-Csv c:\temp\DeviceStats.csv

Powershell - send email with attached file


$date = Get-Date
$file = "C:\temp\report.html"

$smtpServer = "mai.yourdomain.com"
$att = new-object Net.Mail.Attachment($file)
$msg = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpServer)
$msg.From = "sender@yourdomain.com"
$msg.To.Add("receiver.yourdomain.com")
$msg.Subject = "report from " + $date
$msg.Body = "see attachment"
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()



$outfile = “c:\temp\sent.txt”

“Email Sent at ” + $date | out-file $outfile