Podcast
Questions and Answers
What is the function of a terminal emulator?
What is the function of a terminal emulator?
- Directly interfaces with the operating system kernel.
- Manages hardware resources.
- Combines the console and terminal functions. (correct)
- Transforms commands into machine code.
PowerShell cmdlets can be run on any operating system.
PowerShell cmdlets can be run on any operating system.
False (B)
What is the primary role of the Shell in relation to the CLI?
What is the primary role of the Shell in relation to the CLI?
The Shell controls the prompt.
What is the purpose of the 'Get-Process' cmdlet in PowerShell?
What is the purpose of the 'Get-Process' cmdlet in PowerShell?
The Measure
cmdlet provides ______ about collected objects.
The Measure
cmdlet provides ______ about collected objects.
In the context of PowerShell, what does the term 'properties' refer to?
In the context of PowerShell, what does the term 'properties' refer to?
In PowerShell, piping allows you to send text to another command, but not objects.
In PowerShell, piping allows you to send text to another command, but not objects.
What is the purpose of using the | Format-List
command in PowerShell?
What is the purpose of using the | Format-List
command in PowerShell?
In PowerShell, $_ is used for what purpose?
In PowerShell, $_ is used for what purpose?
Using the Where
parameter in a PowerShell command ______ the response to only those objects that match specific command restrictions.
Using the Where
parameter in a PowerShell command ______ the response to only those objects that match specific command restrictions.
In PowerShell, -eq
means 'not equal to'.
In PowerShell, -eq
means 'not equal to'.
Given the PowerShell command $ processes = Get-Process | Where { $_.CPU -gt 20 }
, what does the -gt
operator signify?
Given the PowerShell command $ processes = Get-Process | Where { $_.CPU -gt 20 }
, what does the -gt
operator signify?
What is the primary function of the ForEach
cmdlet in PowerShell?
What is the primary function of the ForEach
cmdlet in PowerShell?
When used with the cmdlet ForEach
, the symbol $_
refers to the ______ .
When used with the cmdlet ForEach
, the symbol $_
refers to the ______ .
The command Set-Location
is used to create new directories in PowerShell.
The command Set-Location
is used to create new directories in PowerShell.
What purpose do aliases serve in PowerShell?
What purpose do aliases serve in PowerShell?
In relation to Get-ChildItem
, what is the alias ls
commonly used for?
In relation to Get-ChildItem
, what is the alias ls
commonly used for?
In Windows PowerShell, hard disks are not recognized as drives.
In Windows PowerShell, hard disks are not recognized as drives.
Match the following PowerShell drives with their corresponding description:
Match the following PowerShell drives with their corresponding description:
Which term best defines a PowerShell cmdlet?
Which term best defines a PowerShell cmdlet?
New-Item, a PowerShell cmdlet, is used for creating ______ and setting its value .
New-Item, a PowerShell cmdlet, is used for creating ______ and setting its value .
Parameters enclosed in square brackets in PowerShell are mandatory.
Parameters enclosed in square brackets in PowerShell are mandatory.
To display the commands that match 'process', which command do you run?
To display the commands that match 'process', which command do you run?
What type of internal structure is used for PowerShell?
What type of internal structure is used for PowerShell?
To continually have the computer go back to redo the Get processes
, PowerShell provides ______.
To continually have the computer go back to redo the Get processes
, PowerShell provides ______.
The correct syntax to assign the number 123 to a variable $t
is $t = '123'
.
The correct syntax to assign the number 123 to a variable $t
is $t = '123'
.
What is a ScriptProperty
in PowerShell?
What is a ScriptProperty
in PowerShell?
Match the type to its definition:
Match the type to its definition:
An object has a type. What does this type define?
An object has a type. What does this type define?
The cmdlet Get-Process
delivers processes, but is not an object.
The cmdlet Get-Process
delivers processes, but is not an object.
What information does GetType()
provide about an object?
What information does GetType()
provide about an object?
______ are for PowerShell-specific functions.
______ are for PowerShell-specific functions.
Methods are exclusive to .NET things.
Methods are exclusive to .NET things.
When would you use a command insteqad of a method?
When would you use a command insteqad of a method?
Match the term with what it does.
Match the term with what it does.
Which of the following is a number?
Which of the following is a number?
A string is not an array.
A string is not an array.
If you have a file with the extension ps1, how do you denote it?
If you have a file with the extension ps1, how do you denote it?
Flashcards
Console
Console
A hardware piece that ran software called the terminal to communicate with the mainframe.
Terminal Emulator
Terminal Emulator
Combines the Console and terminal functions. Its basically a command window.
CLI
CLI
Provides the command line interface (CLI) environment to the user. Accepts commands and passes them to a shell.
Shell
Shell
Signup and view all the flashcards
PowerShell
PowerShell
Signup and view all the flashcards
Properties
Properties
Signup and view all the flashcards
Pipe (|)
Pipe (|)
Signup and view all the flashcards
Get-Process
Get-Process
Signup and view all the flashcards
Where
Where
Signup and view all the flashcards
$_
$_
Signup and view all the flashcards
Echo Command
Echo Command
Signup and view all the flashcards
Set-Location
Set-Location
Signup and view all the flashcards
Aliases
Aliases
Signup and view all the flashcards
PS Drives
PS Drives
Signup and view all the flashcards
New-Item
New-Item
Signup and view all the flashcards
Parameters
Parameters
Signup and view all the flashcards
Where
Where
Signup and view all the flashcards
PowerShell Modules
PowerShell Modules
Signup and view all the flashcards
.NET Libraries
.NET Libraries
Signup and view all the flashcards
CLR
CLR
Signup and view all the flashcards
GetType()
GetType()
Signup and view all the flashcards
Method
Method
Signup and view all the flashcards
Commands
Commands
Signup and view all the flashcards
Void
Void
Signup and view all the flashcards
Integer numbers
Integer numbers
Signup and view all the flashcards
Decimal numbers
Decimal numbers
Signup and view all the flashcards
Dot (.)
Dot (.)
Signup and view all the flashcards
Script Property
Script Property
Signup and view all the flashcards
Note Property
Note Property
Signup and view all the flashcards
types
types
Signup and view all the flashcards
Study Notes
Working with Your OS
- Console started as hardware like a typewriter connecting to a mainframe
- The console ran terminal software, communicating with the mainframe
- A terminal emulator combines the console and terminal functions
- The terminal, provides the CLI environment, accepting commands and passing them to a shell
- PowerShell has cmdlets
Shell Prompts
- The shell controls the prompt
- PS C:\Users\Miles McDonald> indicates PowerShell
- C:\Users\Miles McDonald> indicates cmd
- The prompt can be changed and is followed by the command line
VS Code Intro
- Create a folder structure, for example a folder named ‘OS’ with an internal folder named 'Week 9 Scripts'
- Open VS Code, and open the created folder
- Create new scripts with the icon or by pressing Ctrl+N
- Select PowerShell 7 as the language by clicking the bottom right-hand side corner
- You can get auto-complete by typing: Get-Process
- Right-click and choose ‘Run Selection’ will execute the current line
Displaying and Stopping Processes
- Running Get-Process will display the processes on your device in the terminal window
- Running Get-Process -Name Notepad will display Notepad details; if not running, it gives a red line
- Use Stop-Process -Name Notepad to close Notepad
- Use Control + ~ to Open/close the terminal window in VS Code
- Press F1 to change the colour theme etc
PowerShell Overview
- PowerShell is based on the .Net Framework and incorporates technologies like WMI
- PowerShell is both a shell and a scripting language
- Learning PowerShell is essential for network administrators
- Open PowerShell 7 as administrator and type Get-Help
- Press 'Return'
PowerShell Scripts
- PowerShell is built upon objects
- Important commands include those to filter, sort, and select objects
- Uses the '$_.', syntax familiar with the environment
PowerShell 7 vs. CLI
- CLI (bash or Command Prompt) commands return text
- Using PowerShell CLI commands you can pipe text to another command
Objects
- PowerShell commands return objects
- Objects have properties like name, path, and extension
PowerShell and the 'ls' Command
- Run 'ls' in PowerShell to see results similar to 'bash'
- 'ls' produces a table showing objects in a text format
- Use a pipe (|) to pass the object over for the command Format-List
- Is was taken by PowerShell and formatted via pipe internally as: Is | Format-Table
Get-Process Command
- It lists all the processes running
- Get-Process | Format-List shows the same processes in a different format
Measure Cmdlet
- Measure is short for Measure-Object which tells you the number of objects you get back
- Example: ls | Measure
Important Commands
- Where command limits responses to objects matching the command's restrictions
The $_ Syntax
- 'Where' can run conditions on every item
- $_ refers to the indicated item, telling the computer to fill in the blank
- Dot (.) tells PowerShell to access a specific property on the object
ForEach Cmdlet
- ForEach is used in two different ways
- Ls provides the three objects, which are then given to ForEach
- ForEach runs the code specified for each of the files found
Basic Exercise
- To get processes with an ID above 4000 and calculate the average CPU time, use: Get-Process | where { $.ID -gt 4000} | Sort {$.CPU} | ForEach {$_.CPU} | Measure -Average
Navigating Folders
- Use Set-Location to change the current directory
Aliases
- Aliases are shortcuts for commands
- example, where is an alias for where-object
- Ls is an alias for Get-ChildItem, listing child parts of the current location
PS Drives
- PowerShell organizes everything into drives
- Windows hard disks and devices show up as drives
- Navigate entry to the registry drive using cd HKCU:
- Run find a list Is to show the listing of all the registry entries
Cmdlets
- Cmdlets are PowerShell commands which are objects, using a verb-noun format
New-Item Cmdlet
- Creates new items and sets their value, depending on the location
- Use get-help cmdlet to get information about the New-Item cmdlet
Creating a New File
- Example: New-Item -Path -Name "tcp.txt" -ItemType "file" -Value "transmission control protocol"
- It creates files with the date and length
Content Cmdlets
- Add-Content cmdlet adds to text files
- Get-Content cmdlet gets the content of text files
Discovering Parameters
- Parameters provide details for commands
- The Syntax is: Get-Help (followed by) ‘some command'
- Optional parameters reside within square brackets
Standard and Non-Standard Filtering
- For standard filtering: Is | where { $_.Extension -eq '.txt'}
- Using non-standard filtering is: Ls *.txt
Discovering Cmdlets
- Cmdlet names are logical, following a verb-noun structure
- Complete using tab: Get-Network(tab here)
- This will complete for you if it is right: Get-NetworkSwitchEthernetPort
Command Discovery
- To list potential commands run: Get-Command
- To search commands using wildcards (), run: Get-Command | where {$_.Name -like ‘Get-'}
PowerShell Modules
- PowerShell's internal structure includes modules, libraries, .NET Framework, and CLR
Variables and Operators
- Use variables to perform function results once to reuse them
Creating Variables
- Create variables using $var
- Create an inquiry for Get-Process for CPU usage over 20: $heavyProcesses
- Set variable equal to the return from our Get-Process enquiry
Variable Reuse
- This variable now contains the entire table identified we can reuse this information
ForEach and Variables
- $_ is a variable that allows pulling each (CPU in this case) in turn within ForEach
Turning Text into Numbers
- If $t = "123", it is text
- By running [int] $t it becomes an integer
Further Conversion
- You can convert variables like $students with mathsScore from text to numbers using $students | ForEach {$.mathsScore = [int]$.mathsScore }
Expanded Note Property
- An object is composed of properties
- A ScriptProperty runs a script
- A NoteProperty allows adding after a process is created
PowerShell Script and Columns
- Example of adding a column to a CSV file called ‘students.csv'
- Create a CSV file ‘students.csv' with 3 columns, Name, Maths and English.
- Load the CSV and change text to numbers
- Add a 'sum' column to all objects in the file
- Fill in all those properties
- Save the CSV file
NoTypeInformation
- Without -NoTypeInformation a resultant CSV will have PSCustom type info
- -NoTypeInformation instructs the code to not include the type
- Therefore the data returned to a csv is usable in outside tools
Object Typing
- An Object always has a type attached to it which describes its members, for examples:
- Get-Process delivers processes
- Each process here is an object and each object has same property labels
- Types come from the central areas of the system
Variable data types
- CmdletInfo type is from PowerShell Core
- Process info type is from .NET library
Object Types
- Object types are:
- Property
- NoteProperty
- Method
Common Methods
- GetType() is a common method which returns the type of an object
Methods vs. Commands
- Command is designed specifically for PowerShell
- Method is designed for all .NET things
Commands and Parameters
- bool WaitForExit(int milliseconds); method which returns a bool type of object
- The parameter is an integer and is named 'milliseconds'
- void M(string a, int b); has two parameters, separated by a comma
C# and PowerShell
- Most of .NET and PowerShell is written in C#
- Example Code: string A {get; set;}, the code shows how to look what is in it, and change what's in it
Get-Member and Objects
- Run Get-Member gives one object and will give a clear list of all the members on an object
- Gives a list of all the members on the object if one object is provided, otherwise lists members on the first object.
Member Information
- In get-member, we retrieves the Name, MemberType and the Definitions
- The list is cut off but further down are the methods and Properties listed too.
- Methods can have different variants, example: overloads. moveTO()
- PS C:\Users\Miles McDonald> ls -Directory; Lists directories in current path
Primitive Number Datatypes
- byte (Byte)
- sbyte (SByte)
- short (Int16)
- ushort (UInt16)
- int (Int32)
- uint (UInt32)
- long (Int64)
- ulong (UInt64)
Primitive Decimal Datatypes
- float (Single) - 32 bit
- double (Double) - 64 bit
- Note that Doubles are more precise (because they are bigger)
Boolean Datatype
- isPrimitive Returns: True
String Datatype
- isPrimitive Returns 'False'
One property on the string.
- Int Length {get;}; The length is one property of all strings
Single Quotes vs. Double Quotes
- To break out of the regular usage for quotes, use single quotes
- For example:
- $age = 50'I am $age years old'Will return I am $age years old “I am $($age + 5) years old"Will return I am 55 years old
Concatenation
- $str = 'abc'$str + ''; returns abc
- $str += ''; Returns abc. $str has changed its value to abc.
Arrays
- $myArray = @(), is an empty array, Notice the naming is the same as a variable
- $myArray.count, this will return 0 (zero)
- A string is also an array.
- An array can be considered a list of objects. @('Joe', 'Fred', 'John') is an array with three items.
Array Type
- The type displayed is Object [];is an array containing objects
- [Int] is an array with only integers in it. When you create a strongly typed array, it can only contain values or objects of the specified type.
Open scripts
- A script is a file with some code in it.
- Make a file with the extension ps1
- Two ways to open:1-PSDemoscript.ps1, 2-Rt click on file and select PowerShell 7.
- You can use Visual Studio Code
Studying That Suits You
Use AI to generate personalized quizzes and flashcards to suit your learning preferences.