Examples
The command below displays information about the event logs
on the computer.
PS D:\Users\bala> get-eventlog -list
Max(K) Retain OverflowAction
Entries Name
------ ------
-------------- ------- ----
20,480 0 OverwriteAsNeeded 46,318
Application
15,168 0 OverwriteAsNeeded
0 DFS Replication
20,480 0 OverwriteAsNeeded
0 Hardware Events
512 7 OverwriteOlder
0 Internet Explorer
512 7 OverwriteOlder
0 Key Management Service
16,384 0 OverwriteAsNeeded
4 Microsoft Office Diagnostics
16,384 0 OverwriteAsNeeded 1,078
Microsoft Office Sessions
20,480 0 OverwriteAsNeeded 57,116
System
15,360 0 OverwriteAsNeeded
1,076 Windows PowerShell
This command displays the 3 most recent entries in the
Application event log.
PS D:\Users\bala> get-eventlog -newest 3 -logname
application
Index Time Type
Source EventID Message
----- ---- ----
------ ------- -------
57600 Dec 19 16:47 Info MSSQL$SQLEXPRESS 17403
Server resumed execution after being idle 1792 seconds. Reaso...
57599 Dec 19 16:39 Info MSSQL$SQLEXPRESS 17896
The time stamp counter of CPU on scheduler id 1 is not synchr...
57598 Dec 19 16:19 Info
SecurityCenter 1 The Windows Security Center
Service has started.
This command gets the 10 most recent entries from the System
event log and stores them in the $events variable. The results are then
pipelined to the group-object cmdlet to group them by event id.
PS D:\Users\bala> $events = get-eventlog -logname system
-newest 10
PS D:\Users\bala> $events | group-object eventid
Count Name Group
----- ---- -----
7 7036 {balamurali-b,
balamurali-b, balamurali-b, balamurali-b...}
1 14204 {balamurali-b}
1 134 {balamurali-b}
1 10029 {balamurali-b}
Get-Location
This cmdlet gets information about the current working
location.
The -psProvider parameter outputs a PathInfo object
representing the current working location for the specified providers.
The -psDrive parameter outputs a PathInfo object
representing the current working location for the specified drives.
The -stack and - stackName parameters outputs a StackInfo
object representing the current and specified stack respectively.
Examples:\
This command displays the current working location.
PS D:\Users\bala> get-location | format-list
Drive : D
Provider :
Microsoft.PowerShell.Core\FileSystem
ProviderPath : D:\Users\bala
Path : D:\Users\bala
Get-PSProvider
This cmdlet gets information about the specified Windows
PowerShell provider.
The -psProvider parameter specifies the name or names of the
Windows PowerShell providers about which to retrieve information.
Examples
This command displays a list of all available Windows
PowerShell providers.
PS D:\Users\bala> get-psprovider
Name
Capabilities Drives
----
------------ ------
Alias
ShouldProcess {Alias}
Environment
ShouldProcess {Env}
FileSystem Filter,
ShouldProcess {C, D, E, F...}
Function ShouldProcess
{Function}
Registry
ShouldProcess {HKLM, HKCU}
Variable
ShouldProcess {Variable}
Certificate ShouldProcess
{cert}
This command displays a list of all Windows PowerShell
providers with names that begin with the letters 'fi' or 're'.
PS D:\Users\bala> get-psprovider fi*, re* |
format-list
Name : FileSystem
Drives : {C, D, E, F...}
Path :
Home : D:\Users\bala
Description :
Capabilities : Filter, ShouldProcess
ImplementingType :
Microsoft.PowerShell.Commands.FileSystemProvider
AssemblyInfo :
Name : Registry
Drives : {HKLM, HKCU}
Path :
Home :
Description :
Capabilities : ShouldProcess
ImplementingType :
Microsoft.PowerShell.Commands.RegistryProvider
AssemblyInfo :
Get-Service
The Get-Service cmdlet gets objects representing the services
on the local computer, including running and stopped services. You can direct
Get-Service to get only particular services by specifying the service name or
display name of the services using the -name and -displayname parameters, or you
can use the InputObject parameter to supply a service object representing each
of the services that you want to retrieve.
Examples
This command retrieves all of the services on the system.
The default display shows the status, service name, and display name of each
service.
PS D:\Users\bala> get-service
Status Name DisplayName
------ ---- -----------
Running AeLookupSvc Application Experience
Stopped ALG Application Layer
Gateway Service
Stopped Appinfo Application
Information
Stopped AppMgmt Application Management
............
............
This command retrieves services with service display names
that begin with "WMI".
PS D:\Users\bala> get-service -displayname wmi*
Status Name DisplayName
------ ---- -----------
Stopped wmiApSrv WMI Performance Adapter
These commands display only the services with service names
that begin with "RPC" from a list of service names stored in the $services
variable.
PS D:\Users\bala> $myservice = get-service
PS D:\Users\bala> get-service -inputobject $myservice
-include RPC*
Status Name DisplayName
------ ---- -----------
Stopped RpcLocator Remote Procedure Call (RPC)
Locator
Running RpcSs Remote Procedure Call
(RPC)
This command displays only the services that are currently
running.
PS D:\Users\bala> 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
Get-PfxCertificate
This cmdlet gets an object representing each specified .pfx
certificate file. A .pfx file includes both the certificate and a private
key.
The -filePath parameter specifies the full path to the .pfx
file of the secured file.
Examples
The following command gets information about the
www.fabrikam.com.pfx certificate available with the VS2008 training kit.
PS D:\Users\bala> $fp =
"D:\VS2008TrainingKit\Labs\CardSpace\Setup\scripts\certs\www.fabrikam.com.pfx"
PS D:\Users\bala> get-pfxcertificate -filepath $fp |
format-list
Subject : CN=www.fabrikam.com, O=Fabrikam,
L=Redmond, S=Washington, C=US
Issuer : CN=Adatum, DC=ntdev, DC=corp,
DC=microsoft, DC=com
Thumbprint :
D47DE657FA4902555902CB7F0EDD2BA9B05DEBB8
FriendlyName : Fabrikam
NotBefore : 20-05-2006 05:12:33
NotAfter : 10-03-2011 23:57:56
Extensions : {System.Security.Cryptography.Oid,
System.Security.Cryptography.Oid, System.Security.Cryptography.Oid,
System.Security.Cryptography.Oid...}
Get-ExecutionPolicy
This cmdlet gets the execution policy that is effective in
the shell. The execution policy is determined by the user preference that you
set by using Set-ExecutionPolicy and the Group Policy settings for the Windows
PowerShell execution policy. The default is "Restricted."
This cmdlet has no special parameters except that it supports
the common parameters like -Verbose, -Debug, -ErrorAction, -ErrorVariable, and
-OutVariable.
Examples
This command gets the current execution policy for the
shell.
PS D:\Users\bala> get-executionpolicy
Restricted
Get-AuthenticodeSignature
This cmdlet gets information about the Authenticode signature
in a file. If the file is not signed, the information is retrieved, but it the
fields are blank.
The -filePath parameter specifies the path to the file being
examined. Wildcards are permitted, but they must lead to a single file. The
parameter name ("-FilePath") is optional.
Examples
This command gets information about the Authenticode
signature in the myconsole.psc1 file. It uses the Filepath parameter to specify
the file.
PS D:\> get-authenticodesignature
d:\users\bala\myconsole.psc1
Directory: D:\users\bala
SignerCertificate
Status Path
----------------- ------
----
UnknownError myconsole.psc1
Get-Host
This cmdlet gets a reference to the current console host
object. Displays Windows Powershell version and regional information by default.
This cmdlet can be used to access objects that enable you to manipulate the
console user interface.
Examples
This command displays information about the current
shell.
PS D:\> get-host
Name : ConsoleHost
Version : 1.0.0.0
InstanceId :
4610a822-efa5-45bc-9530-d42eca72f88b
UI :
System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture : en-IN
CurrentUICulture : en-US
PrivateData :
Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
This command assigns the host object to a variable and looks
at various properties and methods of the host object.
PS D:\> $ho = get-host
PS D:\> $ho | get-member
TypeName:
System.Management.Automation.Internal.Host.InternalHost
Name MemberType Definition
---- ----------
----------
EnterNestedPrompt Method System.Void
EnterNestedPrompt()
Equals Method System.Boolean
Equals(Object obj)
ExitNestedPrompt Method System.Void
ExitNestedPrompt()
GetHashCode Method System.Int32
GetHashCode()
GetType Method System.Type
GetType()
get_CurrentCulture Method
System.Globalization.CultureInfo get_CurrentCulture()
get_CurrentUICulture Method
System.Globalization.CultureInfo get_CurrentUICulture()
get_InstanceId Method System.Guid
get_InstanceId()
get_Name Method System.String
get_Name()
get_PrivateData Method
System.Management.Automation.PSObject get_PrivateData()
get_UI Method
System.Management.Automation.Host.PSHostUserInterface get_UI()
get_Version Method System.Version
get_Version()
NotifyBeginApplication Method System.Void
NotifyBeginApplication()
NotifyEndApplication Method System.Void
NotifyEndApplication()
SetShouldExit Method System.Void
SetShouldExit(Int32 exitCode)
ToString Method System.String
ToString()
CurrentCulture Property
System.Globalization.CultureInfo CurrentCulture {get;}
CurrentUICulture Property
System.Globalization.CultureInfo CurrentUICulture {get;}
InstanceId Property System.Guid
InstanceId {get;}
Name Property System.String
Name {get;}
PrivateData Property
System.Management.Automation.PSObject PrivateData {get;}
UI Property
System.Management.Automation.Host.PSHostUserInterface UI {get;}
Version Property
System.Version Version {get;}
This command resizes the Windows PowerShell window to 50
pixels by 50 pixels.
PS D:\> $h = get-host
PS D:\> $win = $h.ui.rawui.windowsize
PS D:\> $win.height = 50
PS D:\> $win.width = 50
PS
D:\> $h.ui.rawui.set_windowsize($win)
Get-Variable
This cmdlet gets the Windows PowerShell variables in the
current console. You can retrieve just the values of the variables by specifying
the ValueOnly parameter and you can filter the variables returned by name.
The parameters -name specifies the name of the variable. The
-valueOnly parameter gets only the value of the variable. The -scope limits the
variables retrieved to the specified scope. The value can be a named scope:
"global", "local", or "script", "private" or it can be a number relative to the
current scope (0 through the number of scopes where 0 is the current scope and
increasing the number by 1 moves to the parent scope of the current scope).
Examples
This command displays variables with names that begin with
the letter 'm'. The value of the variables is also displayed.
PS D:\> get-variable m*
Name Value
----
-----
MaximumHistoryCount 64
MaximumAliasCount 4096
MaximumFunctionCount 4096
MyInvocation
System.Management.Automation.InvocationInfo
MaximumErrorCount 256
MaximumDriveCount 4096
MaximumVariableCount 4096
This command displays only the values of the variables with
names that begin with the letter 'm'.
PS D:\> get-variable m* -valueonly
64
4096
4096
MyCommand : get-variable m* -valueonly
ScriptLineNumber : 0
OffsetInLine : 0
ScriptName :
Line :
PositionMessage :
InvocationName :
PipelineLength : 2
PipelinePosition : 1
256
4096
4096
This command gets information about the variables that begin
with either the letter 'C' or the letter 'P'. The results are piped to the
Sort-Object cmdlet, sorted by name and displayed.
PS D:\> get-variable -include c*, p* | sort-object
name
Name Value
---- -----
ConfirmPreference High
ConsoleFileName
PID 2412
PROFILE
D:\Users\bala\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
ProgressPreference Continue
PSHOME
D:\Windows\System32\WindowsPowerShell\v1.0
Get-TraceSource
This cmdlet gets the trace sources for Windows PowerShell
components that are currently in use. You can use the data to determine which
Windows PowerShell components you can trace. When tracing, the component
generates detailed messages about each step in its internal processing.
Developers use the trace data to monitor data flow, program execution, and
errors. The tracing cmdlets were designed for Windows PowerShell developers, but
are available to all users.
The name parameter gets only the specified trace sources.
Wildcards are permitted. The parameter name ("-Name") is optional.
Examples
This command gets all of the trace sources that have names
that include "provider".
PS D:\> get-tracesource *provider*
Options Name Listeners
Description
------- ----
--------- -----------
None CmdletProviderAt {Default} The
attribute that declares that a class is an implementation of ..
None ProviderConfigur {Default}
ProviderConfigurationEntry
None ProviderCommandA {Default} The APIs
that are exposed to the Cmdlet base class for manipulati..
None CmdletProviderIn {Default} The
APIs that are exposed to the Cmdlet base class for manipulati..
None ProviderIntrinsi {Default}
The APIs that are exposed to the Cmdlet base class for manipulati..
None CoreCommandProvi {Default} The
namespace navigation tracer
None AliasProvider {Default}
The CmdletProvider for shell aliases
None CmdletProviderCl {Default} The
namespace provider base classes tracer
None CmdletProviderCo {Default} The
context under which a core command is being run.
None EventLogLogProvi {Default}
EventLogLogProvider
None EnvironmentProvi {Default} The
core command provider for environment variables
None FileSystemProvid {Default} The
namespace navigation provider for the file system
None VariableProvider {Default} The
core command provider for shell variables
None RegistryProvider {Default} The
namespace navigation provider for the Windows Registry
None CertificateProvi {Default}
The core command provider for certificates
None HelpProvider {Default}
HelpProvider
None AliasHelpProvide {Default}
AliasHelpProvider
None HelpProviderWith {Default}
HelpProviderWithCache
None CommandHelpProvi {Default}
CommandHelpProvider
None ProviderHelpProv {Default}
ProviderHelpProvider
None HelpFileHelpProv {Default}
HelpFileHelpProvider
None HelpProviderWith {Default}
HelpProviderWithFullCache
None FaqHelpProvider {Default}
FaqHelpProvider
None GlossaryHelpProv {Default}
GlossaryHelpProvider
None GeneralHelpProvi {Default}
GeneralHelpProvider
None DefaultHelpProvi {Default}
DefaultHelpProvider
None SessionStateProv {Default}
Providers that produce a view of session state data.
PS D:\> get-tracesource registryprovider
Options Name Listeners
Description
------- ----
--------- -----------
None RegistryProvider {Default} The
namespace navigation provider for the Windows Registry
This command gets all of the Windows PowerShell components
that can be traced.
PS D:\> get-tracesource
Options Name Listeners
Description
------- ----
--------- -----------
None SingleShell
{Default} SingleShell
None MshSnapinLoadUnl {Default}
Loading and unloading mshsnapins
None MshConsoleInfo {Default}
MshConsoleInfo object that is constructed from a console file.
None PSSnapInInfo {Default}
PSSnapInInfo
Get-ChildItem
This cmdlet gets the items in one or more specified
locations. If the item is a container, it gets the items inside the container,
known as child items. You can use the Recurse parameter to get items in all
child containers A location can be a file system location, such as a directory,
or a location exposed by another provider, such as a registry hive or a
certificate store.
The -path parameter specifies a path to one or more
locations. Wildcards are permitted. The default location is the current
directory (.).
The -include parameter retrieves only the specified items.
Enter a path element or pattern, such as "*.txt".
This parameter is effective only when the command includes
the Recurse parameter or the path leads to th e contents of a directory, such
as C:\Windows\*, where the wildcard character specifies the contents of the
C:\Windows directory.
The -exclude parameter omits the specified items. Enter a
path element or pattern, such as "*.txt". Wildcards are permitted.
The -name parameter retrieves only the names of the items in
the locations. If you pipe the output of this command to another command, only
the item names are sent.
Examples
This command gets the child items in the current location. If
the location is a file system directory, it gets the files and
sub-directories in the current directory. If the item does not have child items,
this command returns to the command prompt without displaying anything.
The default displays lists the mode (attributes), last write
time, file size (length), and the name of the file. The valid values for mode
are: d (directory), a (archive), r (read-only), h (hidden), and s (system).
PS D:\Users\bala> get-childitem
Directory:
Microsoft.PowerShell.Core\FileSystem::D:\Users\bala
Mode LastWriteTime Length Name
---- ------------- ------
----
d-r-- 23-11-2007 13:25:13
Contacts
d-r-- 09-08-2008 08:53:24
Desktop
d-r-- 09-08-2008 19:34:21
Documents
d-r-- 25-03-2008 12:38:51
Downloads
d-r-- 11-08-2008 14:29:19
Favorites
This command retrieves all of the .txt files in the current
directory and all of the subdirectories. The Include parameter specifies the
file name extension. The Recurse parameter directs Windows PowerShell to
retrieve objects recursively, and indicates that the subject of the command is
the specified directory and its contents.
PS D:\Users\bala> get-childitem -include *.txt
-recurse
Directory:
Microsoft.PowerShell.Core\FileSystem::D:\Users\bala\Desktop\Systems -
Training\Courses
Mode LastWriteTime Length Name
---- -------------
------ ----
-a--- 07-09-2005 14:58:22 3825
sourcecode.txt
Directory:
Microsoft.PowerShell.Core\FileSystem::D:\Users\bala\Desktop\Mahatma\articles
Mode LastWriteTime Length Name
---- -------------
------ ----
-a--- 11-12-2007 15:02:00 82690 Who is
mahatma?.txt
-a--- 11-12-2007 15:44:43 20144 Does Gandhi
live now?(1949).txt
-a--- 01-12-2007 18:24:39 74229 Are we all
Gandhians?.txt
..........
..........
This command lists the .txt files in the windows directory,
except for those whose names start with the letter M.
PS D:\Users\bala> get-childitem c:\windows\*.txt
-exclude m*
Directory:
Microsoft.PowerShell.Core\FileSystem::C:\windows
Mode LastWriteTime Length Name
---- -------------
------ ----
-a--- 22-11-2007 23:08:28 1924
OEWABLog.txt
-a--- 08-01-2008 17:13:28 27686
SchedLgU.Txt
-a--- 22-11-2007 23:12:25 666071
setuplog.txt
-a--- 22-11-2007 22:31:53 264
UPGRADE.TXT
This command retrieves all of the registry keys in the
HKEY_LOCAL_MACHINE\SOFTWARE key in the registry of the local computer.
PS D:\Users\bala> get-childitem
registry::hklm\software\adobe
Hive:
Microsoft.PowerShell.Core\Registry::hklm\software\adobe
SKC VC Name Property
--- -- ----
--------
1 0 Acrobat Reader {}
1 0 Repair {}
Get-ItemProperty
This cmdlet is used to retrieve the properties of a specified
item.
The -path and -name parameters are used to specify the path
to the item or items and the name of the property or properties to retrieve.
Examples
This command displays information about the C:\Windows
directory.
PS D:\Users\bala> get-itemproperty c:\windows
Directory:
Microsoft.PowerShell.Core\FileSystem::C:\
Mode LastWriteTime Length Name
---- ------------- ------
----
d---- 13-12-2007 20:03:51
windows
This command gets the properties of the myalias.txt file. The
result is piped to the format-list cmdlet to display the output as a list.
PS D:\Users\bala> get-itemproperty myalias.txt |
format-list
Directory:
Microsoft.PowerShell.Core\FileSystem::D:\Users\bala
Name : myalias.txt
Length : 4442
CreationTime : 03-03-2008 11:53:17
LastWriteTime : 03-03-2008 11:53:17
LastAccessTime : 03-03-2008 11:53:17
VersionInfo :
This command displays the value name and data of each of the
registry entries contained in the CurrentVersion registry subkey.
PS D:\Users\bala> get-itemproperty
hklm:\software\mycompany
PSPath :
Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software\mycompany
PSParentPath :
Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\software
PSChildName : mycompany
PSDrive : HKLM
PSProvider : Microsoft.PowerShell.Core\Registry
config : 1
This command is same as the above example except that it
displays the value name and data of the Config registry entry contained in the
Mycompany entry in the registry.
PS D:\Users\bala> get-itemproperty
hklm:\software\mycompany -name 'config' | format-list 'config'
Get-Content
This cmdlet gets the content of the item at the location
specified by the path, such as the text in a file. It reads the content one line
at a time and returns an object for each line.
The parameter -path specifies the path to an item and
-totalCount specifies how many lines of content are retrieved.
The -readCount is an important parameter that specifies how
many lines of content are sent through the pipeline at a time. The default
value, 0, sends all of the content at one time.This parameter does not change
the content displayed, but it does affect the time it takes to display the
content. As the value of ReadCount increases, the time it takes to return the
first line increases, but the total time for the operation decreases. This can
make a perceptible difference in very large items.
|