PowerShell – Telegram
PowerShell
820 subscribers
214 photos
4 videos
1 file
802 links
Task automation and configuration management framework #PowerShell
Download Telegram
Classes

A class in .NET might be considered to be the recipe used to create a type. A type is a representation of something, anything.

Classes are created using the Class keyword.
An instance of the Car class can be created using ::new().

class Car { }
$car = [Car]::new()

What type results when you create classes like above?

#quiz
🎁 Grab the latest PowerShell 7.1 Preview 4 release!

- Breaking Changes
- Engine Updates and Fixes
- General Cmdlet Updates and Fixes

Check full list of changes on GitHub
Classes

Properties are used to describe different information about an object.
A hashtable can be used to fill the properties of an object when the object is created.

class Car {
$Make
[string] $Model
}
$car = [Car]@{
Make = 'Volkswagon'
Model = 'Golf'
}

Properties which do not have an explicit type name defined are given the type System.Object.

$car | gm -name Make

#quiz
Classes

Methods are used to execute predefined actions. This may be changing a property of the object itself, or a change to the the thing the object represents, or it might be used to generate something new from the object.

class File {
[string] $Path
Create() {
New-Item -Path $this.Path -ItemType File 
}
}

$file = [File]@{
Path = "c:\temp\test.txt"
}
$file.Create()

Will you see any output in the shell?

#quiz
Get-Command can also be used to analyze executable and DLL files.
Classes

A method in a class can be assigned one or more arguments.
The return keyword must be used to return a value from the method

class File {
[string] $Path
[void] Create([String] $content) {
New-Item -Path $this.Path -ItemType File -Value $Content -Force
}
[String[]] ReadAll() {
return [System.IO.File]::ReadAllLines($this.Path)
}
}

#quiz
📘 PowerShell by Mistake

Learn PowerShell by reviewing "broken" code and discovering the answers. A great complement to any structured learning effort! Honor System: Pay what you feel this book is worth and what fits your budget.

#ebook #free
📻 Windows Terminal 1.0 with Kayla Cinnamon

What if there was one place for Windows CLI commands, PowerShell, even SSH and Bash? There is, and its called Windows Terminal.

#podcast #terminal