PowerShell for Windows Admins


December 17, 2015  1:31 PM

Outputting AD data to CSV

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Active Directory, Powershell

Back in this post https://richardspowershellblog.wordpress.com/2014/12/29/using-givenname-and-surname-instead-of-samaccountname/

I showed how to get AD information using afirst and last names rather than the samAccountName.

A question came up about reading from a CSV containing a list of names and outputting the results to another CSV.

My preference would be to create a single PowerShell pipeline.

Import-Csv -Path ./adtest.csv |
foreach  {
$fname = $psitem.GivenName
$lname = $psitem.Surname
Get-ADUser -Filter {GivenName -eq $fname -and Surname -eq $lname} -Properties * |
select SamAccountName, Division, Office, City
} |
Export-Csv aduserslist.csv –NoTypeInformation

Read the CSV and pipe to foreach. Get the data and push onto pipeline. Export to CSV.

December 15, 2015  2:33 PM

Recent Announcements from the PowerShell Team

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The PowerShell Team have made a number of interesting  announcements recently:

Feedback is moving from Connect to User Voice

http://blogs.msdn.com/b/powershell/archive/2015/12/14/improving-the-powershell-feedback-experience-with-uservoice.aspx

This is where you give your feedback and report bugs

For programmers the PowerShell reference assemblies are now available through Nuget.org

http://blogs.msdn.com/b/powershell/archive/2015/12/12/powershell-sdk-reference-assemblies-available-via-nuget-org.aspx

Meaning you can use Install-Package to get them.  Assemblies for PowerShell versions 3.0, 4.0 and 5.0 are available

The tests the PowerShell Team uses to test new PowerShell builds are available on GitHub

http://blogs.msdn.com/b/powershell/archive/2015/12/07/powershell-tests-released-on-github.aspx

Useful to understand how PowerShell is tested and to get a deeper insight into PowerShell


December 11, 2015  6:04 AM

Formating multiple outputs

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

Using Get-WmiObject and Get-Service you can do this:

PS> Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode

StartMode
———
Auto
PS> Get-Service -Name BITS | select Status

Status
——
Running

If you try running the two commands in a script, in ISE or even like this:

PS> Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode; Get-Service -Name BITS | select Status

StartMode
———
Auto

You only get the first result.  If you reverse the order of the commands:

PS> Get-Service -Name BITS | select Status; Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode

Status
——
Running

You still get the first result.

When you run a pipeline the results are automatically piped to Out-Default. The formatting system then decides to use a table format because you have less than 5 properties.

When the commands are run individually each calls Out-Default individually. When the 2 commands are run in a script (or other option that causes them to execute together) the formatting takes its direction from the first object it receives and because the second object doesn’t have matching properties nothing is displayed.

The answer is to manually force each command to pipe to out-default

PS> Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’BITS’” | select StartMode | Out-Default; Get-Service -N
ame BITS | select Status | Out-Default

StartMode
———
Auto

Status
——
Running

OR

PS> Get-Service -Name BITS | select Status | Out-Default; Get-WmiObject -Class Win32_Service -Property StartMode -Filter “Name=’B
ITS’” | select StartMode | Out-Default

Status
——
Running

StartMode
———
Auto

This is a common issue that users trip over as they learn PowerShell.


December 8, 2015  11:34 AM

PowerShell Day at Manning

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Books, Powershell

Manning have a set of PowerShell books on their deal of the day for 9 December 2015. The deal includes:

Windows PowerShell in Action, Third Edition
PowerShell and WMI
PowerShell Deep Dives
Powershell in Depth, Second Edition
PowerShell in Practice
Learn Windows PowerShell in a Month of Lunches, Second Edition

The deal will go live at Midnight US ET and will stay active for ~48 hours.

Get 50% off these books with code dotd120915au at manning.com


November 27, 2015  8:53 AM

PowerShell Summit 2016–agenda complete

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The agenda is complete on the event web site. We still have to finalise the sessions from the PowerShell Team but they will be giving a number of sessions.


November 25, 2015  7:06 PM

A deal of the day you’ll not want to miss

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Books, Powershell

PowerShell in Action, Third Edition is part of Manning’s Deal of the Day on Friday 27 November – see www.manning.com for details on the day –

Half off my book Windows PowerShell in Action, Third Edition. Use code dotd112715au at https://www.manning.com/books/windows-powershell-in-action-third-edition


November 20, 2015  10:45 AM

Creating Registry Key

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
CIM, Powershell, WMI

I had a question left on the blog asking how to create a registry key. My preferred method is to use the CIM class = StdRegProv. Its a static class so you don’t need to create an object

[uint32]$hklm = 2147483650
$newkey = ‘SOFTWARE\NewKey’

Invoke-CimMethod -ClassName StdRegProv -MethodName CreateKey -Arguments @{hDefKey = $hklm; sSubKeyName = $newkey}

Define the variables for the HIVE in this case HKLM – local machine . Notice that the value has to be an unsigned integer.

The key you want to create is just the path to the key. if you need to create multiple levels of subkeys they will all create from a single path.

Then use Invoke-CimMethod to call the CreateKey method on StdRegProv. The hash table in Arguments parameter has the method parameter names and appropriate values.

If everything works you’ll get a return value of 0.

How did I know which parameters the method took?

I used Get-CimClass but that’s a story for another post.


November 20, 2015  4:13 AM

PowerShell column

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

The first in a regular(ish) series of articles has been published on the TechNet UK Blog.

http://blogs.technet.com/b/uktechnet/archive/2015/11/20/why-you-need-to-learn-powershell.aspx

Covering all things PowerShell related the articles will be appearing every 3-4 weeks.


November 20, 2015  4:09 AM

Windows Server 2016 TP4

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Windows Server 2016

A new technology preview for Windows Server 2016 has just been released. Available from all good Microsoft download sites.


November 19, 2015  2:49 PM

Splatting and Default parameters

Richard Siddaway Richard Siddaway Profile: Richard Siddaway
Powershell

One thing you don’t hear much about is default parameters.

Consider this

Get-CimInstance -ClassName Win32_LogicalDisk -Filter “DeviceId = ‘C:'”

A pretty standard use of CIM.

Now think if you have to do this across a number of machines on a regular basis. Typing could get a bit tedious.

You could use splatting:

$params = @{
ClassName = ‘Win32_LogicalDisk’
Filter = “DeviceId = ‘C:'”
}

Get-CimInstance @params

Create a hash table of parameter names and values and use that to reduce your typing. Because its a hash table you can modify as required to use other classes or Filters

An alternative is to use default parameters

$PSDefaultParameterValues = @{
‘Get-CimInstance:ClassName’ = ‘Win32_LogicalDisk’
‘Get-CimInstance:Filter’ = “DeviceId = ‘C:'”
}

Get-CimInstance

Use the $PSDefaultParameterValues variable to hold your default values. Note how the cmdlet and parameter are defined. You can then call the cmdlet and the default parameters and their values are applied.

If you want to override the default values you may have to do it for all of the default values for a cmdlet – in the above case the Filter is nonsensical if applied to Win32_OperatingSystem so you’d have to do this

Get-CimInstance -ClassName Win32_OperatingSystem -Filter “Manufacturer LIKE ‘%'”

Used with a bit of care splatting and default parameters are a good way to save typing


Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: