0% found this document useful (0 votes)
14 views

Module 2 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Module 2 (1)

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

A.

PSProviders and PSDrives

Windows PowerShell providers provide access to data and components that would not
otherwise be easily accessible at the command line. The data is presented in a consistent
format that resembles a file system drive. Windows PowerShell providers are Microsoft .NET
Framework-based programs that make the data in a specialised data store available in
Windows PowerShell so that you can view and manage it. The data that a provider exposes
appears in a drive, and you access the data in a path like you would on a hard disk drive. You
can use any of the built-in cmdlets that the provider supports to manage the data in the
provider drive. And, you can use custom cmdlets that are designed especially for the data.
The providers can also add dynamic parameters to the built-in cmdlets.

PowerShell Providers allow you to navigate and manipulate data beyond just the file system.
Examples include:

 FileSystem: Access and manipulate files and folders.


 Registry: Navigate and manipulate the Windows registry.
 Certificate: Work with certificate stores.
 Environment: Access environment variables.
 Alias: Manage PowerShell aliases

These are parameters that are available only when you use the cmdlet with the provider data.

1. Built-in providers
Windows PowerShell includes a set of built-in providers that you can use to access the
different types of data stores. You can also create your own Windows PowerShell providers,
and you can install providers that others develop. To list the providers that are available in
your session, do as:

Get-PSProvider

2. Installing and removing providers


 Providers are typically installed via Windows PowerShell modules.
 Importing the module loads the provider into your session.
 You cannot uninstall the built-in providers.
 You can uninstall providers loaded by other modules.
 You can unload a provider from the current session.
 You can do this by using the Remove-Module cmdlet.
 This cmdlet does not uninstall the provider, but it makes the provider unavailable in
the session
3. PowerShell Drive
A Windows PowerShell drive is a data store location that you can access like a file system
drive in Windows PowerShell.

 The Windows PowerShell providers create some drives for you, such as the file
system drives (including C: and D:), the registry drives (HKCU: and HKLM:), and
the certificate drive (Cert:), and you can create your own Windows PowerShell.
 These drives are very useful, but they are available only within Windows PowerShell.
You cannot access them by using other Windows tools, such as File Explorer or
Cmd.exe.

4. Working with PSDrives


Windows PowerShell uses the noun, PSDrive, for commands that work with Windows
PowerShell. For a list of the Windows PowerShell in your Windows PowerShell session, use
the Get- PSDrive cmdlet.

Get-PSDrive
 Working with PSDrives File system drives are a subset of the Windows PowerShell.
You can identify the file system drives by the Filesystem entry in the Provider
column. To see the syntax of the Get- PSDrive cmdlet, type a Get-Command
command with the Syntax parameter:

Get-Command -Name Get-PSDrive -Syntax

 The PSProvider parameter lets you display only the Windows PowerShell that are
supported by a particular provider. For example, to display only the Windows
PowerShell that are supported by the Windows PowerShell FileSystem provider, type
a Get- PSDrive command with the PSProvider parameter and the FileSystem value:

Get-PSDrive -PSProvider FileSystem

 To view the Windows PowerShell that represent registry hives, use the PSProvider
parameter to display only the Windows PowerShell that are supported by the
Windows PowerShell Registry provider:

Get-PSDrive -PSProvider Registry


5. Types of PSProviders
i. FileSystem Provider
The File System Provider in PowerShell is a built-in provider that allows you to navigate and
manipulate the file system, including files and directories as if they were a hierarchical file
structure. This provider is part of PowerShell's provider model, which provides a unified way
to interact with various data stores.

a. Navigating Directories
o Use Get-Location or pwd
o Use Set-Location or cd
Example-

i. sl .\Desktop\ - to move between directories.


ii. sl \ - to change to root directory.
iii. sl .. – to change one step from the current directory.
iv. Set-Location D: or sl D: - to change from C to D drive.
b. Listing Files and Directories
o Get-ChildItem or dir or ls or gci
c. Creating files and Directories
o UseNew-Item
Example-
i. Create a file in the current directory

New-Item -Path . -Name "testfile1.txt" -ItemType "file" -Value


"This is a text string."
ii. Create a directory
New-Item -Path "c:\" -Name "logfiles" -ItemType
"directory"
d. Moving or renaming files and directories
o Use Copy-Item
Example-
i. Copy a file to the specified directory

Copy-Item "C:\Wabash\Logfiles\mar1604.log.txt" -Destination


"C:\Presentation"
ii. Copy directory contents to an existing directory

Copy-Item -Path "C:\Logfiles\*" -Destination "C:\Drawings" -


Recurse
[ NOTE- If the path `C:\Drawings` doesn't exist the cmdlet copies all the files from
the `Logfiles` > folder into a single file `C:\Drawings`.]

iii. Copy directory and contents to a new directory

Copy-Item -Path "C:\Logfiles" –Destination "C:\Drawings\


Logs" -Recurse
[NOTE- If the Path includes `*`, all the directory's file contents, including the
subdirectory > trees, are copied to the new destination directory. For example: > >
`Copy-Item -Path "C:\Logfiles*" –Destination "C:\Drawings\Logs" -Recurse`

iv. Copy a file to the specified directory and rename the file

Copy-Item -Path "E:\testfile1\testfile2\abc.txt" -Destination "C:\


Drawings123\xyz.txt" -Recurse
e. Deleting files and directories
o Use Remove-Item or rm
Example-

i. rm .\sample\ - delete a directory


ii. rm a, b, c – delete multiple directories

f. To check file and directory properties


o Use Get-ItemProperty
Example-

i. Get information about a specific directory

Get-ItemProperty C:\Windows
ii. Get the properties of a specific file

Get-ItemProperty C:\Test\Weather.xls | Format-List

g. Working with file content


o Use Get-Content

Example-

i. Get content of a file


Get-content E:\testfile1\testfile2\abc.txt
h. Filtering
o Use Where-Object

i. Sorting
o Use Sort-Object

VARIABLES
Variables in Windows PowerShell (and all other scripting and programming languages) let
you store the output of something so that you can use it later.

• A variable name starts with a dollar sign ($) and can be followed by nearly any character.

• A small set of characters have special meaning to Windows PowerShell, so Windows


PowerShell provides a way to make variable names that include even these.
• One can store the result of any pipeline or command in a variable to use it later.

• If that command generates simple data (such as a number or string), then the variable
contains simple data.

• If the command generates rich data (such as the objects that represent system processes
from the Get Process cmdlet), then the variable contains that list of rich data.

• If the command (such as a traditional executable) generates plain text (such as the output of
traditional executable), then the variable contains plain text.

In addition to variables that you create, Windows PowerShell automatically defines several
variables that represent things such as the location of your profile file, the process ID of
Windows PowerShell, and more.

• For a full list of these automatic variables, type Get-Help about_automatic_variables.

1. $result=2+2
$result

2. $processes=get-process
$processes.count

You can access this information later, or even pass it down the pipeline as though it
were the output of the original command:
$processes | Where-Object { $_.ID -eq 0 }

3. "Hello World" > test


Get-Content test
Get-Content c:test

ACCESS ENVIRONMENT VARIABLES

You want to use an environment variable (such as the system path or the current user’s name)
in your script or interactive session.

1. To list all environment variables, list the children of the env drive:

Get-ChildItem env:
2. To get an environment variable using a more concise syntax, precede its name with
$env:
$env:variablename
3. To get an environment variable using its provider path, supply env: or Environment::
to the Get-ChildItem cmdlet:
Get-ChildItem env:variablename
Get-ChildItem Environment::variablename

B. Enable remoting in a single remote machine


 PSRemoting lets you run powershell commands or entire powershell sessions on a
remote windows system.
1. Enable windows remote management service on your client machine(machine 1), to
which you want to establish connection.

2. Open powershell(admin) on your server(machine 2) and check whether your remote


machine(client) is configured correctly using-
Test-WSMan "RemoteComputerIPAddress"

3. Type the cmdlet

Enter-PSSession -ComputerName "RemoteComputerName" -Credential “Credentials”


The Enter-PSSession cmdlet in PowerShell is used to start an interactive session with a remote
computer using PowerShell Remoting. Once you establish a session, you can run commands on the
remote machine as if you were logged in locally. This cmdlet uses the Windows Remote
Management (WinRM) service, which is based on the WSMan protocol.

The “Windows10” at the start of next line indicates your server is succesfully connected to
your client.

4. Type any commands now to work with your remote client.


Example
C. Active Directory
 Every IT Administrator faces a number of Active Directory Management challenges which
includes managing user accounts in Active Directory almost every day.
 Configuring user properties manually is extremely time-consuming, tiresome, and error-
prone, especially in a large, complex Windows network.
 Active Directory administrators and IT managers mostly have to perform repetitive and
mundane tasks which often end up eroding into their productive or free times.
 Moreover, accomplishing these tasks using the native tools or Windows PowerShell also
demands a deeper knowledge in Active Directory Management, and related technologies are
not trouble or complexity free by any means.
 AD Manager Plus is one simple, hassle-free web-based solution for all Active Directory
Management challenges, safe with secure authentication and performs all actions with just
mouse clicks.
 This Active Directory management tool allows administrators to design templates to manage
all Active Directory account creation and modification processes.
 Moreover, through its web-interface, this AD management software offers administrators an
absolute control over their Active Directory environment.
 Keep in mind that in order to use these Windows PowerShell scripts, you must import the
module for interacting with AD — the Active Directory Module for Microsoft Windows
PowerShell.
 This module was introduced in Windows Server 2008 R2 and is enabled by default in
Windows Server 2012 and later. You can get the full list of AD module cmdlets by running
the following command:
 To create an AD group, use the New- ADGroup cmdlet. You can get its syntax by running the
following command:

You might also like