Files
Powershell-Training/1.Basic/Learn Powershell in Less than 2 hours.md

286 lines
5.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Learn Powershell in Less Than 2 hours
**Everything in Powershell is an Object**
## Widows Powershell ISE
To be able to write Powershell script you have a free IDE
## CMDLET
Predifine functions are names CommandLet
Verb-Noun -Parameters
If you want to see all the Cmdlet available
```
Get-Command -CommandTyper Cmdlet
```
## PSVersionTable
you can see the version of powershell with the command
```powershell
$PSVersionTable
```
## ExecutionPolicy
To be able to see if you can run your Powershell scripts
```powershell
Get-ExecutionPolicy
# Work only has administrator
Set-ExecutionPolicy remoteSigned
```
### Write-Host
For writing in the console
```powershell
Write-Host "Hello world"
Write-Host "Hello world" -NoNewline
Write-Host "Hello world" -BackgroundColor "Blue"
```
## GetHelp
Print all the help articles from microsoft
```powershell
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
You can chain commands with "|"
```powershell
Command-1 | Command-2
"May the force be with you!" | Out-File forewithwho.txt
```
## Variables
They start with a "$"
you can use strings,floating points, integer and boolean
```powershell
$FavCharacter = "Spongebob"
$FavCharacter | Out-File favcharacter.txt
$FavCharacter.GetType()
$x = 5
$y = 3
$x + $y
# output: 8
$FavCharacter | Select-Object -Property *
# output all the Property of the variable
Get-Member -InputObject $FaveCharacter
# output all the mehtods you can use
```
## Arrays
A collection of variables
```powershell
$Jedi = @('Obi-Wan Kenobi', 'Luke Skywalker', 'Master Yoda', 'Mace Windu')
$Jedi.GetType()
# output Object[]
$Jedi[0]
# output : Obi-Wan Kenobi
$Jedi[0].Length
# output 14
$Jedi += 'Qui-Gon Jin'
# add a variable to the array
```
## hash table
Specify keys
```powershell
$FellowshipBeta = @{key1 = "item1"; key2 = "item2"}
$Fellowship = @{"Wizard" = "Gandalf"; "Elf" = "Legolas"; "Hobbit" = "Frodo"}
$Fellowship.Add("Dwarf", "Gimli")
# for adding an element
$Fellowship.Set_Item("Dwarf", "Bilbo")
# changing an element
$Fellowship."Dwarf"
$Fellowship.["Dwarf"]
# retrieve an element
$Fellowship.Remove("Dwarf")
# Remove an element
```
## Read-Host
for having user iput
## Evaluatig
```powershell
$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 users first name.
-Surname "Skywalker"
The users 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 users 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
```