Monday, January 9, 2012

Finding Driver Information using PowerShell

 

PowerTip of the Day, from PowerShell.com:

driverquery.exe returns all kinds of information about installed drivers, but the information seems a bit useless at first:

PS> driverquery.exe /V

 

Module Name     Display Name                  Description                   Driver Type         Start Mode State          St

============ ================  ================== ============= ========== =======  ==

1394ohci           OHCI-konformer 1394-Ho OHCI-konformer 1394-Ho Kernel    Manual         Stopped     OK
20.11.2010 11:44:56    C:\Windows\system32\drivers\1394ohci.sys         4.096

This console application does support a parameter called /FO CSV. This formats the information as a comma-separated list:

PS> driverquery.exe /v /FO CSV

"Module Name","Display Name","Description","Driver Type","Start Mode","State","S

tatus","Accept Stop","Accept Pause","Paged Pool(bytes)","Code(bytes)","BSS(bytes

)","Link Date","Path","Init(bytes)"

"1394ohci","OHCI-konformer 1394-Hostcontroller","OHCI-konformer 1394-Hostcontrol

ler","Kernel ","Manual","Stopped","OK","FALSE","FALSE","4.096","200.704","0","20

.11.2010 11:44:56","C:\Windows\system32\drivers\1394ohci.sys","4.096"

Now here's the scoop: Powershell can not only read the results from console application. When the output is CSV, it can even convert the raw text automagically into objects. So, with just one line, you get tremendous information about drivers (thanks to Stephen Owen for pointing us to this):

PS> driverquery.exe /v /FO CSV | ConvertFrom-CSV | Select-Object 'Display Name',

 'Start Mode', 'Paged Pool(bytes)', Path

 

Display Name                     Start Mode               Paged Pool(bytes)       Path

------------                     ----------                -----------------      ----

OHCI-konformer 1... Manual                         4.096                                       C:\Windows\syste...

Microsoft ACPI-T... Boot                              90.112                                     C:\Windows\syste...

ACPI-Energieanze... Manual                         4.096                                       C:\Windows\syste...

(...)

 

Thanks

Creating Multiline Strings

 

PowerTip of the Day, from PowerShell.com:

You probably know what this line produces:

'Hello' * 12

Right, you get 12 times the string you submitted. If you wanted new lines instead, a minor adjustment will do the job:

,'Hello' * 12

The comma puts the string into an array, and when you multiply arrays, you get additional array elements. Simply use Out-String to turn that into a single multi-line string:

PS> $text = ,'Hello' * 12

PS> $text.GetType().FullName

System.Object[]

PS> $text.Count

12

PS> $text = ,'Hello' * 12 | Out-String

PS> $text.GetType().FullName

System.String

PS> $text

Hello

Hello

Hello

(...)

 

Thanks

Adding New Lines to Strings

 

PowerTip of the Day, from PowerShell.com:

 

In a previous tip you learned that text arrays can easily be multiplied. The same is true for assignment operators such as +=. When you apply this operator to a string, it appends a text:

PS> $text = "Hello"

PS> $text += "World"

PS> $text

HelloWorld

When you apply it to a string array, it adds new lines:

PS> $text = @()

PS> $text += "Hello"

PS> $text += "World"

PS> $text

Hello

World

PS> $text | Out-String

Hello

World

So, to construct multiline text throughout your script, start with  @() to create an empty array, then add all the lines to this array using +=. When you are done, pipe the array to Out-String to get one multiline string. Easy.

 

Thanks

Enumerating Registry Keys

 

Enumerating Registry Keys

To enumerate all subkeys in a Registry key, you might be using a line like this:

PS> Dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall | Select-Object -expand PSPath

Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

Microsoft.PowerShell.Core\Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

It will return the PowerShell-style path including the provider name, not just the Registry path. If you used the -Name parameter, on the other hand, you just get the subkey names and no path at all:

PS> Dir HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall -Name

AddressBook

Connection Manager

(...)

The easiest way to get the true Registry path is Resolve-Path:

PS> Resolve-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*

Path

----

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

The resulting paths still use PowerShell drives. To get the native Registry paths, try this:

PS> Resolve-Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object -ExpandProperty ProviderPath

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\AddressBook

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Connection Manager

(...)

Thanks

Use Out-GridView Requirements

 

PowerTip of the Day, from PowerShell.com

 

Out-GridView is a great way to present results in a “mini-Excel” sheet:

Get-Process | Out-GridView

However, Out-GridView has two requirements:.NET Framework 3.5.1 and the built-in script editor ISE must both be installed. ISE is not installed by default on Windows Servers. So, if you want  to use Out-GridView on server products, you will need to make sure you install the ISE feature.

On a Server 2008 R2, you could enable ISE by using PowerShell:

Import-Module ServerManager

Add-WindowsFeature PowerShell-ISE

Thanks

Change Service Start Mode the PowerShell Way

 

 

When you list services with Get-Service, you will find that a lot of properties may seem to be missing. You can still set such properties when you pipe a service to Set-Service. The following code will change the start mode of the Spooler service (provided you have sufficient privileges):

Get-Service spooler | Set-Service -StartupType Automatic

 

PowerTip of the Day, from PowerShell.com

 

Thanks

Tuesday, December 13, 2011

Writing Registry Key Default Values

 

PowerTip of the Day, from PowerShell.com:

 

If you need to set the default value for a Registry Key, you can  use either of these approaches:

Set-ItemProperty -Path HKCU:\Software\Somekey -Name ‘(Default)’ -Value MyValue

Or, you can just do this:

Set-Item -Path HKCU:\Software\Somekey -Value MyValue

 

thanks