Skip to content

Instantly share code, notes, and snippets.

@jhamilton09
Last active February 7, 2016 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhamilton09/48a7fa4e685c82f23feb to your computer and use it in GitHub Desktop.
Save jhamilton09/48a7fa4e685c82f23feb to your computer and use it in GitHub Desktop.
PowerShell Basics examples
#PowerShell Building Blocks
#Single-line comments begin with a pound sign.
<# Multi-line comments can span between angle brackets and pound signs.
Just like this.
The syntax highlighter in PowerShell ISE colors comments green so they're easy to see. #>
#Output
#Output to the monitor
Write-Host "Hello World"
#More generic output, better for file output
Write-Output "Hello World"
#Clear the monitor of previous output
Clear-Host
#Input
# Prompt with a question, input the answer, clear the screen, and display it back to the user.
Write-Output "What is your favorite color?"
$color = Read-Host
Clear-Host
Write-Host "Your favorite color is" $color"." -ForegroundColor $color
#Variables
#Save content to a variable to work with later.
$total = 2+2
Write-Host $total
#Auto-increment or decrement a variable
$total = 0
$total++
Write-Host $total
$total++
Write-Host $total
$total--
Write-Host $total
$total--
Write-Host $total
#You can also read system environment variables like the computer name
$computername = $env:COMPUTERNAME
Write-Host $computername
#... or PowerShell version
$psversion = $psversiontable.PSVersion
Write-Host $psversion
#Piping
#Get-Process returns the active processes (like TaskMgr)
Get-Process
#Piping the results to select-object allows us to filter just the columns we want.
Get-Process | Select-Object ProcessName
#Format output
#Format the output to be a table by piping the results to format-table
Get-Process | Format-Table
#Format the output to be a list by piping the results to format-list
Get-Process | Format-List
#These can be shortened to aliases, ft for table and fl for list
Get-Process | ft
Get-Process | fl
#Create a folder
New-Item C:\scripting -type Directory
#Write out to a file
$total = 1+1
Write-Output $total | Out-file C:\scripting\demo.txt
#Write out to a file without overwriting the contents, instead adding to it, or appending.
$total = 1+1+1
Write-Output $total | Out-file C:\scripting\demo.txt -Append
#Work with files
#Conditional, Does file exist?
If (Test-Path "C:\Scripting\demo.txt"){
#Demo file exists, read file
$read = Get-Content "C:\Scripting\demo.txt"
#Create new file
New-Item -path C:\scripting -name demo2.txt -type "file" -value "File existed"
} else {
#Demo file does not exist, create file
Write-Output "Creating file" | Out-file C:\scripting\demo.txt
#or
New-Item -path C:\scripting -name demo2.txt -type "file" -value "File did not exist"
}
#Creating a new folder
New-Item C:\scripting2 -type Directory
#Moving file from scripting folder to scripting2
Move-Item -path C:\scripting\demo.txt -destination C:\scripting2\demo.txt
#Copying file from scripting folder to scripting2
Copy-Item -path C:\scripting\demo2.txt -destination C:\scripting2\demo2.txt
#Delete files and directories
remove-item -path C:\Scripting\demo2.txt
remove-item -path C:\Scripting
#or delete folder and subfolder/files
remove-item -path C:\scripting2 -Recurse
#Ping
Test-Connection server.fqdn
if (Test-Connection server.fqdn -quiet) {
#Server is available to ping
} else {
#Server is unavailable to ping
}
#Loops until variable is no longer less than 5
$total = 0
Do {
Write-Host $total -ForegroundColor "Magenta"
$total++
} While ($total -lt '5')
#Loops until variable is greater than 4
$total = 0
Do {
Write-Host $total -ForegroundColor "Yellow"
$total++
} Until ($total -gt '4')
#Functions
function calculateTip {
$totalwtip = $args[0] * 1.15
Write-Output "Total of $args with 15% tip is $totalwtip"
}
calculateTip 25
calculateTip 50
exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment