OS-Lab6
OS-Lab6
One of the key features of PowerShell is its ability to work with objects rather than just
text. This object-oriented approach allows for more flexibility and easier manipulation of
data. PowerShell also integrates with the .NET framework, enabling access to a wide
range of functions and libraries.
PowerShell scripts are stored in.ps1 files. You cannot run a script by simply double-
clicking a file; this design helps avoid accidental harm to your systems. Instead, to
execute a script, right-click it and choose Run with PowerShell.
In addition, there is a policy that restricts script execution. You can check this policy by
running the Get-ExecutionPolicy command in PowerShell.
Restricted — No scripts are allowed. This is the default setting, so you will see it the
first time you run the command.
AllSigned — You can run scripts signed by a trusted developer. Before executing, a
script will ask you to confirm that you want to run it.
RemoteSigned — You can run your own scripts or scripts signed by a trusted
developer.
To start working with PowerShell, you’ll need to change the policy setting from
Restricted to RemoteSigned using the Set-ExecutionPolicy RemoteSigned command.
A cmdlet always consists of a verb (or a word that functions as a verb) and a noun,
separated with a hyphen (the “verb-noun” rule).
Alexandria University - Faculty of Science
Course: Operating Systems
Lecturer: Dr. Ahmed Younes
Lab: 6
—--------------------------------------------------------------------------------------------------
For example, some of the verbs include:
You can list all cmdlets by executing the Get Help -Category cmdlet, Or to find a
specific command Get-Help Get-Process -Examples
You can also create your own custom cmdlets. Each cmdlet has several parameters
that customize what it does. The PowerShell ISE will automatically suggest all valid
parameters and their types after you type a cmdlet and a hyphen (-)
For example, the following cmdlet shows all services whose names start with “W”: Get-
Service -Name W*
You can also use aliases, which are shortened cmdlet names. For instance, instead of
Get-Help you can use just Help. Try running the following two commands and see
whether you get the same result:
Start-Process notepad
start notepad
Alexandria University - Faculty of Science
Course: Operating Systems
Lecturer: Dr. Ahmed Younes
Lab: 6
—--------------------------------------------------------------------------------------------------
Similarly, to stop this process, you can use either of the following commands:
1.4 Comments:
Leaving comments in a script will help you better understand what the script does.
A string comment is a single line that starts with a number sign (#); block comments
spread across multiple lines, starting and ending with number signs and angle brackets:
1.5 Pipes:
A pipe passes data from one cmdlet to another. I used a pipe earlier to get all properties
of an object.
For example, if you execute the following script, you’ll get all services sorted by their
status:
You can also use a pipe to output text to a file using a script like the following:
You can use multiple pipes. For instance, the following script lists all services, with the
first pipe excluding stopped services and the second pipe limiting the list to display
names only: