I would like to propose we should all function as adults at some point in life. maybe not 24 hours a day but enough to take care of ourselves.
PowerShell is an area that need you to be functioning to preserve your sanity and time, I will cover simple & advanced functions.
A function can save you a ton of typing and it could do quite a bit and be called using the infamous tab completion!
Simple Function Example:
Function Test-Google { Try{ $result=Test-Connection google.com if ($result -ne $null){ write-host "Google is up and reachable" -ForegroundColor Green} } Catch{write-host "Google Error Occured" -ForegroundColor Red} }
Advanced Function Example:
Function Test-NetworkConnectivity { [cmdletbinding()] param( [parameter(ValueFromPipelineByPropertyName,ValueFromPipeline)][Object[]]$Website =@( "google.com","Bing.com") ) Begin{} Process{ FOREACH($SITE IN $Website) { Try{ $result=Test-Connection -ComputerName $site if ($result -ne $null){ write-host "$site is up and reachable" -ForegroundColor Green} } Catch{write-host "$site Error Occured" -ForegroundColor Red} } } }
Take the time to explore tab completion. this advanced function has way more capability and any questions?