Professional Documents
Culture Documents
Presentation: T3 - Group 6
Presentation: T3 - Group 6
T3 - GROUP 6
ANUSHA (TL)
SUBHADEEP DEB ROY
NANDINI
SAI CHARAN
VISHAL
INTRODUCTION
TO
POWERSHELL latest version : 7.1.4
August 12, 2021
What is PowerShell ?
● Easy to use
● No Compiling
● Not case-sensitive
● PowerShell is a new environment for Windows system
administrators that allows them to use a more modern
command-line environment to manage Windows
For what powershell is used ?
● Get-Help
● Get-Command
● Get-PSDrive
● Get-Service
● Format-List
What can we do using PowerShell
To list all current available variables, run the ls This is very similar to the list variable command
variable:* command.
Print Variable:
To write to a .txt file, use the Out-File command: To export data to a .csv file, use the Export-Csv
command:
Print Variable:
And to write to an HTML file, use the ConvertTo- To read a file that we exported, we use the Get-
Html command: Content cmdlet:
Powershell Array
Array creation
When creating arrays, be aware of the default PowerShell behavior. If you create an array with multiple elements, PowerShell will
create an array, as you intend. For example, put a few numbers into an array and then check the data type of the variable:
However, if you put just one value in a variable, then PowerShell will not create an array:
If a comma is used as a binary operator, then a normal array is created; if it is used as a unary operator, the array has just one element.
For example, here is how we can get an array consisting of one element:
Array List
If you will modify or search an array frequently, you can use the ArrayList class, which is designed to let you easily add, remove,
and search for items in it:
Multidimensional Arrays
Multidimensional arrays are variables that can be used to store information in a table without having to write it to a real database. It
looks like a hash table, but it can store different types of information, such as strings and integers.
Sort Array
If an array contains only one data type, you can sort the values using the Sort method:
Add and Remove Item
Array Length Array Contains
In order to handle each element in an array one after another, we need to make a loop using the foreach
operator.
LOOPS
&
CONDITIONAL STATEMENTS
Scripts can make decisions. How?
Foreach
While Loop
For Loop
Do While
If Statement
A Foreach loop reads a set of objects (iterates) and completes when it’s
finished with the last one.
You can write multiple lines of code inside the while block and run it several
times until the criteria met.
ForEach(ITEM in COLLECTION)
{
CODE BLOCK
}
While(text_expression)
{
Statement_1
Statement_2
..
Statement_n
}
While condition to check if number is less han 8 or not
To check for yes or no condition
SYNTAX:
for(<init>;<condition>;<repeat>)
{
<statement lists>
}
Using a for loop gives more control over limits and conditions on when
the code should exit. This is compared with the foreach or the ForEach-
Object cmdlet, where iteration stops after the last item in the collection
was processed.
Update progress using for loop
First is the Do keyword, then the script block that I want to “do.”
Then comes the While keyword, and the condition that is evaluated to
determine if another loop will occur.
SYNTAX:
Do{
..
Block of statements
..}
while(condition);
To check if the tool is present or not using do while
SYNTAX:
if(condition) {
// Executes when the condition
is true
}else {
// Executes when the condition
is false
}
To check if the banks are open or not using loop
Switch function in PowerShell is used to handle multiple If statements or in
other terms it is replacement of multiple conditions If/Else If/Else.
SYNTAX:
Switch (<Value>)
{
<Condition1> {Action1}
<Condition2> {Action2}
}
Break Statement
Continue Statement
Unlike break statement, the continue statement does not terminate a loop
● The faster calculation for big data, saving a lot of manual labor
Syntax:
To get Different
disk partitions.
Accessing Remote Computer Services Namespaces
Remove-Wmiobject:
Remove-WmiObject
[-Class] <String>
[-Impersonation <ImpersonationLevel>]
[-Authentication <AuthenticationLevel>]
[-Locale <String>]
[-EnableAllPrivileges]
[-Authority <String>]
[-Credential <PSCredential>]
[-ThrottleLimit <Int32>]
[-ComputerName <String[]>]
[-Namespace <String>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
Examples:
To Terminate a running process
● Get-Event
● Get-EventSubscriber
● New-Event
● Register-WmiEvent
● Remove-Event
● Unregister-Event
Exploring WMI Events
Register-WmiEvent
Get-Event
$process = "calc.exe"
$WMIQuery = "SELECT *
FROM
__InstanceCreationEvent
WITHIN 5 WHERE
TargetInstance ISA
'Win32_Process' AND
TargetInstance.Name =
'$Process'"
Register-WmiEvent -Query
$WMIQuery `
-SourceIdentifier "Process
$Process"
Summary:
•WMI is one of the most powerful tools available to Windows
administrators.
•WMI and PowerShell provide access to the event engine so that you can
be alerted when things happen. You can also configure an action to be
performed when an event is triggered.
•WMI is a standards-based management toolset that’s installed by default
in modern Windows systems. It supplies a large number of classes that
correspond to physical entities in your systems, such as CPU, operating
system, or installed application.
•Due to the complexity, the lack of documentation, and the difficulty in using
it in the past it's not popular.
Exploring the Active
Directory Module
What is an
Active Directory?
Active Directory is a
Centralized Hierarchical
Directory Database.
Active Directory Services:
Domain
Domain
How to Install Active Directory using Powershell
1. Type Start PowerShell and press Enter within the Command Prompt window to open a new Windows
PowerShell console window.
2.Type Add-WindowsFeature AD-Domain-Services and press Enter to install
Active Directory Domain Services.
After Installing
To add domain name:
2. Type Install-ADDSForest -DomainName training.com -InstallDNS and press Enter to create a new forest and domain named training.com. After this,
the server needs a restart and will now be a Domain Controller
3. The domain controller promotion will complete and the server will be rebooted finalizing the process.
DNS will be also installed
How to create an Organization Unit (OU)
2. Create and compile the script for creating the desired OU. Execute the script
in PowerShell.
For demo, we are using :
You can create an AD user in a specific OU by using the -path parameter in New-ADuser.
We used Get-ADUser -Filter * -SearchBase “ou=DemoOU, dc=training, dc=com” to check the users inside DemoOU
To set properties of a user:
There are several properties to change, for example we are using few of them.
new-adgroup -name Demo -description "Demo Group" -groupscope Global -path "OU=DemoOU ,dc=training, dc=com"
The Scope of the group along with the name and path is required
To check the availability of a group:
We use ‘Get-AdGroup’ followed by group name
To Add members in a group:
We use Add-AdGroupMember followed by the names of users
To check members inside a group:
We use the cmdlet Get-ADGroupMember followed by groupname
Get-ADGroupMember -identity Demo
To add members in bulk:
Use ADD-AdGroupMember followed by the usernames.
To Remove members from a group:
We use Remove-ADGroupMember cmdlet followed by username/usernames
To Change GroupScope of a Group:
We use Set-ADGroup followed by groupname and new groupscope
To Remove a group:
We use Remove-ADGroup followed by group name
To Delete an OU:
We use Remove-ADOrganizationUnit followed by the path of the OU
To Delete an OU (Where accidental prevention is marked):
We have to run the script followed by path of the Organization Unit
Get-ADOrganizationalUnit -Identity 'OU=DemoOU2,DC=training,DC=com' |
Set-ADObject -ProtectedFromAccidentalDeletion:$false -PassThru |
Remove-ADOrganizationalUnit -Confirm:$false