Archives : May-2024

Using Powershell with Active Directory Hint: Enable View > Advanced Features in the AD GUI to view all the properties a user has. Import the AD Module Import-Module ActiveDirectory Add a new AD User #New-ADUser -Name “<Full Name>” -GivenName “<Firstname>” -Surname “<Lastname>” -SamAccountName “<shortname>” -UserPrincipalName “<email@address.tld>” -Path “OU=Administration,OU=Staff,OU=STARWARS,DC=starwars,DC=com” -AccountPassword(ConvertTo-SecureString “<Password>” -AsPlainText -force) -Enabled $true New-ADUser ..

Read more

cmdlets Run these directly from the command line or use them in your scripts. New-Item Creating Files C:\path\to\somewhere\> New-Item -path C:\scripts\myfile.txt -type “file” -value “Here is some text” C:>cd \scripts C:> ls Mode LastWriteTime Length Name … -a—- MM/DD/YYYY H:mm 17 myfile.txt … C:\scripts\> New-Item -path .\myotherfile.txt -type “file” -value “Here is some text” C:> ..

Read more

Functions Define a function function myFunction { param( #define parameters $MyParameter ) # Do some stuff Write-Host “You set MyParameter to” $MyParameter } myFunction -MyParameter 10 Note: above function prints ‘You set MyParameter to 10’ Cmdlet Bindings If the required parameter is not set when calling, the function will might fail. You can avoid this ..

Read more

Powershell Loops For Loops For Loop (Using Counters) For(InitValue; Comparison; Increment){ … } $myFriends = @(‘Rob’, ‘Paul’, ‘Sam’, ‘Kyle’) For($counter=0; $counter -le ($myFriends.Length – 1); $counter++){ Write-Host $myFriends[$counter] “is my friend.” } Note: Would have been better to use -lt and actual length instead of the extra math, but showing what can be done here. ..

Read more

Conditions Terrinaries -eq Equal To -ne Not Equal To -le Less than or Equal To -lt Less than -ge Greater than or Equal To -gt Greater than Boolians -and And: ($x -eq $y -and $i -ne $j) -or Or: ($x -eq $y -or $i -ne $j) -xor: ($x -eq $y -xor $i -ne $j) -not ..

Read more

User Input Read-Host $userName = Read-Host -Prompt “What is your name? More Advanced: Write-Host “What is your favorite system?” Write-Host “1. NES” Write-Host “2. Atari” Write-Host “3. N64” Write-Host “4. Wii” Write-Host “5. Switch” $favSystem = Read-Host -Prompt “What is your favorite gaming syste..

Read more

Powershell Variables Variables Variable Objects All variables are Objects   Variable declaration: $VarName Note: These are NOT case sensitive! $MyVar is the same as $myvar Static variables (aka read-only constants): $true (True, 1) $false (False, 0) Note: You cannot assign variable to to True or False. Use their variable names. Get Variable type Note: Here ..

Read more