Skip to content

Instantly share code, notes, and snippets.

View royashbrook's full-sized avatar

Roy Ashbrook royashbrook

View GitHub Profile
# Confirm system access:
"user1","user2" | Get-ADUser -properties * | ft SamAccountName, DisplayName, Enabled
# Disable:
"user1","user2" | Disable-ADAccount
# If processing a change for folks who should be terminated, easier to do this:
$userlist = "user1","user2"
$userlist | Get-ADUser -properties * | ft SamAccountName, DisplayName, Enabled
$userlist | Disable-ADAccount
# Add/Remove Email Access For Another Mailbox:
# First get a session to work in, unless you have the tools installed
$Session = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri http://mailserver/PowerShell/ `
-Authentication Kerberos
Import-PSSession $Session
# setup users
$a = 'user@email.com' #who's mailbox is it?
# Get speed of last command:
($hi = (Get-History)[-1]) | %{ $hi;$hi.EndExecutionTime - $hi.StartExecutionTime}
# check get-history for id-1 to get index if you want a specific task
# Save task as xml (and delete them, or a list of them):
$sn="server"
$tn="taskname"
schtasks /query /s $sn /tn $tn /xml one > "cleanup_job-$tn.xml"
schtasks /delete /s $sn /tn $tn
Function Get-Uptime {
Param ( [string] $ComputerName = $env:COMPUTERNAME )
$os = Get-WmiObject win32_operatingsystem -ComputerName $ComputerName -ErrorAction SilentlyContinue
if ($os.LastBootUpTime) {
$uptime = (Get-Date) - $os.ConvertToDateTime($os.LastBootUpTime)
Write-Output ("Last boot: " + $os.ConvertToDateTime($os.LastBootUpTime) )
Write-Output ("Uptime : " + $uptime.Days + " Days " + $uptime.Hours + " Hours " + $uptime.Minutes + " Minutes" )
}
else {
Write-Warning "Unable to connect to $computername"
# Generate a..z in powershell
([char[]]([int][char]'a'..[int][char]'z'))
@'
username,fullname
user1,"user one"
user2,"user two"
'@ | ConvertFrom-Csv | foreach {
$AD = Get-ADUser -Identity $_.username
New-Object PSObject -Property @{
UserName = $_.username
FullName = $_.fullname
Name = $AD.Name
New-ADUser newdomainadmin -OtherAttributes @{displayname="new domain admin person"}
Set-ADAccountPassword newdomainadmin -Reset -NewPassword (ConvertTo-SecureString -AsPlainText "plaintextpassword" -Force )
Get-ADGroup "Domain Admins"
Get-ADGroup "Domain Admins" | Add-ADGroupMember -Members (Get-ADUser newdomainadmin )
Get-ADGroup "Domain Admins"
#set the user to fix, this should match the duplicate that was sent from local AD
$sam = "user1"
$old = "$sam@olddomain.com"
$new = "$sam@newdomain.com"
#show the users
Get-MsolUser -UserPrincipalName $old
Get-MsolUser -UserPrincipalName $new
#remove the old user
Remove-MSOLuser -UserPrincipalName $old -Force
#remove it from recyclebin
-- temp table variable
declare @tbl table (
a varchar(2)
)
insert @tbl
select 'v0'
union all select 'v1'
union all select 'v2'