diff --git a/1.Basic/10.Function.ps1 b/1.Basic/10.Function.ps1 new file mode 100644 index 0000000..5c7e3a3 --- /dev/null +++ b/1.Basic/10.Function.ps1 @@ -0,0 +1,9 @@ +function Test { + [CmdletBinding()] #turns into adv function + param( + [int32] $PingCount + ) + Test-Connection google.com -Count $PingCount +} + +Test diff --git a/1.Basic/11.Function_exception.ps1 b/1.Basic/11.Function_exception.ps1 new file mode 100644 index 0000000..ab61c2a --- /dev/null +++ b/1.Basic/11.Function_exception.ps1 @@ -0,0 +1,3 @@ +Throw "it's a trap" + +Write-Error -Message "It' a trap" -ErrorAction Stop diff --git a/1.Basic/12.Function_error.ps1 b/1.Basic/12.Function_error.ps1 new file mode 100644 index 0000000..369f2de --- /dev/null +++ b/1.Basic/12.Function_error.ps1 @@ -0,0 +1,10 @@ +function Test { + [CmdletBinding()] #turns into adv function + param( + [int32] $PingCount + ) + Test-Connection google.com -Count $PingCount + Write-Error -Message "It' a trap" -ErrorAction Stop +} + +try {Test -ErrorAction Stop} catch {Write-Output "Launch problem!" Write-Output $_} diff --git a/1.Basic/6.for_loops.ps1 b/1.Basic/6.for_loops.ps1 index 091bbfd..d9a21be 100644 --- a/1.Basic/6.for_loops.ps1 +++ b/1.Basic/6.for_loops.ps1 @@ -1,6 +1,5 @@ $HaloPeeps = @('Master Chief', 'Cortana', 'Captain Keys', 'Flood') -For($counter = 0; $counter -le 3; $counter++){ +For($counter = 0; $counter -le ($HaloPeeps - 1 ); $counter++){ Write-Host "Holy smokes. it's" $HaloPeeps[$counter] - } diff --git a/1.Basic/7.for_each.ps1 b/1.Basic/7.for_each.ps1 new file mode 100644 index 0000000..c0ec66a --- /dev/null +++ b/1.Basic/7.for_each.ps1 @@ -0,0 +1,5 @@ +$HaloPeeps = @('Master Chief', 'Cortana', 'Captain Keys', 'Flood') + +Foreach ($peep in $HaloPeeps){ + Write-Host $peep "has arrived!" +} diff --git a/1.Basic/8.While.ps1 b/1.Basic/8.While.ps1 new file mode 100644 index 0000000..d0ec924 --- /dev/null +++ b/1.Basic/8.While.ps1 @@ -0,0 +1,8 @@ +$xmen = @('Wolverine', 'Cyclops', 'Storm', 'Professor x', 'Gambie', 'Dr. Jean Grey') +$counter = 0 + +While ($counter -ne 6){ + Write-Host $xmen[$counter] "is a mutant!" + $xmen[$counter].Length + $counter++; +} diff --git a/1.Basic/9.Do_while.ps1 b/1.Basic/9.Do_while.ps1 new file mode 100644 index 0000000..1669469 --- /dev/null +++ b/1.Basic/9.Do_while.ps1 @@ -0,0 +1,8 @@ +$xmen = @('Wolverine', 'Cyclops', 'Storm', 'Professor x', 'Gambie', 'Dr. Jean Grey') + +$counter = 0 + +Do { + Write-Host $xmen[$counter] "is a mutant" + $counter++ +} While ($counter -ne 6) diff --git a/1.Basic/Learn Powershell in Less than 2 hours.md b/1.Basic/Learn Powershell in Less than 2 hours.md index fdbeb08..d26d00f 100644 --- a/1.Basic/Learn Powershell in Less than 2 hours.md +++ b/1.Basic/Learn Powershell in Less than 2 hours.md @@ -54,6 +54,56 @@ Get-Help Get-Help Write-Host -full ``` +## New-Item + +For creating a new file or folder + +```powershell +# Create a file +New-Item -path C:\Scripts\ewok.txt -tye "File" -value "praise C3PO" + +# Create a folder +New-Itemn -path C:\Scripts -name "deathstar" -type directory +``` + +## Copy-Item + +Copy a file + +```powershell +Copy-Item C:\Scripts\ewok.txt -destination C:\Scripts\deathstar +``` + +## Move-Item + +Change the place of a file + +```powershell +Move-Item -path ".\forewithwho.txt" -destination ".\deathstar\" +``` +## Remove-Item + +Delete a file + +```powershell +Delete-Item -path ewok.txt +``` + +## Test-Path + +Verify if a file exist and return a boolean +can be helpful for a if statement + +```powershell +Test-Path C:\Scipts\deathstar\forewithwho.txt +``` +## Rename-Item + +Rename a file + +```powershell +Rename-Item -path .\forewithwho.txt -newnae forewithme.txt +``` ## Piping @@ -140,3 +190,96 @@ $PokemonCaught = "908" If($PokemonCaught -eg 908) { Write-Host "You're a Pokemon Master! "} ``` +## Active directory + +for importing active directory Commands + +```powershell +Import-Module ActiveDirectory +``` + +##User + + +### New-ADUser + +Create a user in active Directory + +```powershell +New-ADUser -Name "Luke Skywalker" -GivenName "Luke" -Surname "Skywalker" -SamAccountName "Luke Skywalker" -UserPrincipalName "lskywalker@maeds.org" -Path "OU=Administration, OU=Staff, OU=MAEDS,DC=maeds , DC=org" -AccountPassword(ConverTo-SecureString "UncleOwen!" -AsPlainText -force) -Enabled $true +``` + +-Name "Luke Skywalker" +The display name of the user in Active Directory. + +-GivenName "Luke" +The user’s first name. + +-Surname "Skywalker" +The user’s last name. + +-SamAccountName "Luke Skywalker" +The logon name used with older Windows systems (DOMAIN\username). + +-UserPrincipalName "lskywalker@maeds.org" +The modern logon name (email-style login). + +-Path "OU=Administration, OU=Staff, OU=MAEDS, DC=maeds, DC=org" +Specifies the Organizational Unit (OU) where the user will be created. +DC specifies the domain name + +-AccountPassword (ConvertTo-SecureString "UncleOwen!" -AsPlainText -Force) +Sets the user’s password (converted into a secure format). + +-Enabled $true +Enables the account immediately after creation. + +### Get-ADUser + +For having aformation about a user +return basic information + +```powershell +Get-ADUser mrsh + +# we can turn it to a variable +$user = Get-ADUser + +# we can return information +$user.DistguishedName +$user.GivenName + +# extract information +$user.GivenName | Out-File users.txt +``` + +### Set-ADUser + +add or replace informations about a user + +```powershell +Set-ADUser -Identity fbaggins -Surname Tyler +``` +### Set-ADAccountPassword + +Change the password of a user + +```powershell +Set-ADAccountPassword -Identity lskywalker -Reset -NewPassword (ConverTo-SecureString -AsPlainText "OldBen123" -Force) +``` + +### Add-ADGroupMember + +Add a member to a group +```powershell +Add-ADGroupMember - Identity Fellowship - Members fbaggins +``` + +### Remove-ADGroupMember + +Remove a member to the group + +```powershell +Remove-ADGroupMember - Identity Fellowship - Members fbaggins +``` +