Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Server

Windows Server 2008 R2 : Using Windows PowerShell (part 1) - Exploring PowerShell

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
3/15/2011 11:37:23 AM
PowerShell is a powerful tool that enables administrators to manage Windows platform applications and to complete automation tasks. This section sheds some light on how PowerShell’s many uses can be discovered and how it can be used to manage Windows Server 2008 R2.

Exploring PowerShell

Before using PowerShell, you might want to become more familiar with its cmdlets and features. To assist administrators with exploring PowerShell, the PowerShell team decided to do two things. First, they included a cmdlet that functions very similarly to how the UNIX man pages function. Second, they also included a cmdlet that returns information about commands available in the current session. Together, these cmdlets allow a novice to tap into and understand PowerShell without secondary reference materials; explanations of these cmdlets are discussed in the following sections.

Getting Help

The Get-Help cmdlet is used to retrieve help information about cmdlets, aliases, and from help files. To display a list of all help topics this cmdlet supports, enter Get-Help * at the PowerShell command prompt, as shown here:

PS C:\> get-help *

Name Category Synopsis
---- -------- --------
ac Alias Add-Content
asnp Alias Add-PSSnapin
clc Alias Clear-Content
cli Alias Clear-Item
clp Alias Clear-ItemProperty
clv Alias Clear-Variable
cpi Alias Copy-Item
cpp Alias Copy-ItemProperty
cvpa Alias Convert-Path
...

If that list seems too large to work with, it can be shortened by filtering on topic name and category. For example, to get a list of all cmdlets starting with the verb Get, try the command shown in the following example:

PS C:\> get-help -Name get-* -Category cmdlet

Name Category Synopsis
---- -------- --------
Get-Command Cmdlet Gets basic information...
Get-Help Cmdlet Displays information a...
Get-History Cmdlet Gets a list of the com...
Get-PSSnapin Cmdlet Gets the Windows Power...
Get-EventLog Cmdlet Gets information about...
Get-ChildItem Cmdlet Gets the items and chi...
Get-Content Cmdlet Gets the content of th...
...

PS C:\>


After selecting a help topic, that topic can be retrieved by using the topic name as the parameter to the Get-Help cmdlet. For example, to retrieve help for the Get-Content cmdlet, enter the following command:

PS C:\> get-help get-content

After executing this command, a shortened view of the help content for the Get-Content cmdlet is displayed. To view the full help content, include the full switch parameter with the command:

PS C:\> get-help get-content –full

After executing the command with the full switch parameter, you will find that the full help content is divided into several sections. Table 1 describes each of these sections.

Table 1. PowerShell Help Sections
Help SectionDescription
NameThe name of the cmdlet
SynopsisA brief description of what the cmdlet does
DescriptionA detailed description of the cmdlet’s behavior, usually including usage examples
SyntaxSpecific usage details for entering commands with the cmdlet
ParametersValid parameters that can be used with this cmdlet
InputsThe type of input this cmdlet accepts
OutputsThe type of data that the cmdlet returns
NotesAdditional detailed information on using the cmdlet, including specific scenarios and possible limitations or idiosyncrasies
ExamplesCommon usage examples for the cmdlet
Related LinksReferences other cmdlets that perform similar tasks

Get-Command

The Get-Command is used to gather basic information about cmdlets and other commands that are available. For example, when executed, the Get-Command lists all the cmdlets available to the PowerShell session:

PS C:\> get-command

CommandType Name Definition
----------- ---- ----------
Cmdlet Add-Content Add-Content [-Path] <String[...
Cmdlet Add-History Add-History [[-InputObject] ...
Cmdlet Add-Member Add-Member [-MemberType] <PS...
Cmdlet Add-PSSnapin Add-PSSnapin [-Name] <String...
Cmdlet Clear-Content Clear-Content [-Path] <Strin...
Cmdlet Clear-Item Clear-Item [-Path] <String[]...
Cmdlet Clear-ItemProperty Clear-ItemProperty [-Path] <...
Cmdlet Clear-Variable Clear-Variable [-Name] <Stri...
Cmdlet Compare-Object Compare-Object [-ReferenceOb...
...

PS C:\>


Next, to retrieve basic information about a particular cmdlet, you would then include that cmdlet’s name and argument. For example:

PS C:\> Get-Command Get-Process

CommandType Name Definition
----------- ---- ----------
Cmdlet Get-Process Get-Process [[-Name] <String...


PS C:\>


The Get-Command cmdlet is more powerful than Get-Help because it lists all available commands (cmdlets, scripts, aliases, functions, and native applications) in a PowerShell session, as shown in this example:

PS C:\> get-command note*

CommandType Name Definition
----------- ---- ----------
Application NOTEPAD.EXE C:\WINDOWS\NOTEPAD.EXE
Application notepad.exe C:\WINDOWS\system32\notepad.exe


PS C:\>


When using Get-Command with elements other than cmdlets, the information returned is a little different from information you see for a cmdlet. For example, with an existing application, the value of the Definition property is the path to the application. However, other information about the application is also available, as shown here:

PS C:\> get-command ipconfig | format-list *
FileVersionInfo : File: C:\WINDOWS\system32\ipconfig.exe
InternalName: ipconfig.exe
OriginalFilename: ipconfig.exe
FileVersion: 5.1.2600.2180 (xpsp_sp2_rtm.040803-2158)
FileDescription: IP Configuration Utility
Product: Microsoftr Windowsr Operating System
ProductVersion: 5.1.2600.2180
Debug: False
Patched: False
PreRelease: False
PrivateBuild: False
SpecialBuild: False
Language: English (United States)

Path : C:\WINDOWS\system32\ipconfig.exe
Extension : .exe
Definition : C:\WINDOWS\system32\ipconfig.exe
Name : ipconfig.exe
CommandType : Application


With a function, the Definition property is the body of the function:

PS C:\> get-command Prompt

CommandType Name Definition
----------- ---- ----------
Function prompt Write-Host ("PS" + $(Get-Lo...

PS C:\>


With an alias, the Definition property is the aliased command:

PS C:\> get-command write

CommandType Name Definition
----------- ---- ----------
Alias write Write-Output
PS C:\>

With a script file, the Definition property is the path to the script. With a non-PowerShell script (such as a .bat or .vbs file), the information returned is the same as other existing applications.

Managing Services

In PowerShell, a number of cmdlets can be used to manage services on a local machine. A list of these cmdlets is as follows:

  • Get-Service— Used to gather service information from Windows.

  • New-Service— Used to create a new service in Windows.

  • Restart-Service— Used to restart services.

  • Resume-Service— Used to resume suspended services.

  • Set-Service— Used to modify service configurations.

  • Start-Service— Used to start services.

  • Stop-Service— Used to stop services.

  • Suspend-Service— Used to suspend services.

Getting Service Information

When the Get-Service cmdlet is executed, it returns a collection of objects that contains information about all the services that are present on a Windows system. A representation of that object collection is then outputted into a formatted table, as shown in the following example:

PS C:\> get-service

Status Name DisplayName
------ ---- -----------
Running AeLookupSvc Application Experience
Stopped ALG Application Layer Gateway Service
Running AppHostSvc Application Host Helper Service
Stopped Appinfo Application Information
Stopped AppMgmt Application Management
Stopped aspnet_state ASP.NET State Service
Stopped AudioEndpointBu... Windows Audio Endpoint Builder
Stopped AudioSrv Windows Audio
...

To filter the information returned based on the service status, the object collection can be piped to the Where-Object cmdlet, as shown in the following example:

PS C:\> get-service | where-object {$_.Status -eq "Stopped"}

Status Name DisplayName
------ ---- -----------
Stopped ALG Application Layer Gateway Service
Stopped Appinfo Application Information
Stopped AppMgmt Application Management
Stopped aspnet_state ASP.NET State Service
Stopped AudioEndpointBu... Windows Audio Endpoint Builder
Stopped AudioSrv Windows Audio
...

As shown in the preceding example, the Where-Object object cmdlet is used in conjunction with a code block {...}, which is executed as the filter. In this case, the code block contained an expression that filtered the object collection based on services that were “Stopped.” The same type of logic can also be applied to return information about a particular service. For example:

PS C:\> get-service | where-object {$_.Name -eq "DNS"} | fl


Name : DNS
DisplayName : DNS Server
Status : Running
DependentServices : {}
ServicesDependedOn : {Afd, Tcpip, RpcSs, NTDS}
CanPauseAndContinue : True
CanShutdown : True
CanStop : True
ServiceType : Win32OwnProcess


PS C:\>

In the preceding example, the object collection from the Get-Service cmdlet is piped to the Where-Object cmdlet. The filter statement defined script block then instructs the Where-Object cmdlet to return an object for the DNS service. The object that is returned by this cmdlet is then piped to the Format-List cmdlet, which writes a formatted list (containing information about the object) back to the console session.

Note

A shorter method for performing the preceding action is to use the name switch, as shown in the following command: get-service –name DNS.


Managing Service Statuses

To stop a service in PowerShell, the Stop-Service cmdlet is used, as shown in this example:

PS C:\> stop-service -name dns

Notice that when the cmdlet has finished executing, no status information about the service’s status is returned. To gather that information, the passthru switch parameter can be used to pass the object created by a cmdlet through to the pipeline. For example:

PS C:\> start-service -name dns -pass | ft

Status Name DisplayName
------ ---- -----------
Running DNS DNS Server

In the preceding example, the passthru switch parameter is used in conjunction with the Start-Service cmdlet. When the cmdlet has finished executing, thus starting the DNS service, the object is piped to the Format-Table cmdlet, which then displays status information about the DNS service.

Modifying Services

The Set-Service cmdlet is used to change a service’s properties (such as its description, display name, and start mode). To use this cmdlet, either pass it a service object or specify the name of the service to be modified, plus the property to be modified. For example, to modify the startup type of the DNS service, use the following command:

PS C:\> set-service -name DNS -start "manual"

A startup type can be defined as Automatic, Manual, or Disabled. To change a service’s description, a command might look as follows:

PS C:\> set-service -name DNS -description "My Important DNS Service"

Note

The service management cmdlets in PowerShell are not end-alls for managing Windows services. There are a number of areas in which these cmdlets are lacking—for example, not being able to define a service’s logon account or report on its startup type. Luckily, if a more in-depth interface is needed, an administrator can always fall back onto WMI.

Other -----------------
- Windows Server 2008 R2 : Automating Tasks Using PowerShell Scripting - Introduction to PowerShell
- Windows Server 2008 R2 : Automating Tasks Using PowerShell Scripting - Understanding Shells
- Windows Server 2003 : Using DNS Monitoring Tools (part 2) - Monitoring DNS Performance with System Monitor
- Windows Server 2003 : Using DNS Monitoring Tools (part 1) - Using Replication Monitor
- Windows Server 2003 : Using DNS Troubleshooting Tools (part 3) - DNS Debug Log
- Windows Server 2003 : Using DNS Troubleshooting Tools (part 2) - Viewing the DNS Events Log
- Windows Server 2003 : Using DNS Troubleshooting Tools (part 1) - Querying DNS with Nslookup
- Maintaining Windows Server 2008 R2
- Windows Server 2008 R2 : Keeping Up with Service Packs and Updates
- Managing Windows Server 2008 R2 Remotely
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server