Get-ADUser filtering

Saw a question on the forums that revolved around Get-ADUser filtering.

Initial code was like this

Import-Csv .\users.txt |
foreach {
   Get-ADUser -Filter {Name -like $_.Name}
}

which on the face of it seems reasonable. However, you get errors like this

Get-ADUser : Property: ‘Name’ not found in object of type:
‘System.Management.Automation.PSCustomObject’.
At line:3 char:3
+   Get-ADUser -Filter {Name -like $_.Name}
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
     + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft
    .ActiveDirectory.Management.Commands.GetADUser

Change –like to –eq and you’ll still get the error.

This won’t work either:

Import-Csv .\users.txt |
foreach {
   Get-ADUser -Filter {Name -like $($_.Name)}
}

You get messages about path errors.

Import-Csv .\users.txt |
foreach {
   Get-ADUser -Filter {Name -like “$($_.Name)”}
}

will run but won’t return any data.

This will run and return the correct data.

Import-Csv .\users.txt |
foreach {
   $name = $_.Name
   Get-ADUser -Filter {Name -like $name}
}

Alternatively, you can use quotes so that the filter is a string

Import-Csv .\users.txt |
foreach {
   Get-ADUser -Filter “Name -like ‘$($_.Name)'”
}

Another option is to use the LDAP filter syntax

Import-Csv .\users.txt |
foreach {
   $name = $_.Name
   Get-ADUser -LDAPFilter “(Name=$name)”
}

Import-Csv .\users.txt |
foreach {
   Get-ADUser -LDAPFilter “(Name=$($_.Name))”
}

The help file about_activedirectory_filter is well worth reading. It doesn’t seem to be installed with the AD module on Windows Server 2016. You can read it on line at https://technet.microsoft.com/en-us/library/hh531527(v=ws.10).aspx

You’ll also see links to

about_ActiveDirectory  – overview of module

about_ActiveDirectory_Identity

about_ActiveDirectory_ObjectModel

Get-ADUser filtering isn’t the most obvious of topics to get your head round but these examples should help you make your filters work. If you’re building a complex filter build it up a step at a time so you can test each stage.

Posted in PowerShell and Active Directory | Leave a comment

PowerShell Summit 2018: Call for topics

The call for topics for the PowerShell and DevOps Summit 2018 is now open – https://powershell.org/2017/08/01/76318/

We’re looking for sessions (45 or 105 minute) that span the whole range of PowerShell usage and knowledge PLUS sessions on DevOps  practices.

This is your opportunity to speak at the premier PowerShell event of 2018.

Posted in Powershell, Summit | Leave a comment

PowerShell documentation

The home of Microsoft’s PowerShell documentation is changing from MSDN to https://docs.microsoft.com

The PowerShell documentation is currently opened sourced at https://github.com/powershell/powershell-docs

This change makes accessing the documentation easier

Posted in Powershell | 1 Comment

You have to laugh

Sometimes things just happen and you have to laugh.

So I decided I wanted to get back to working with the Windows 10 Insider previews (and Windows Server previews). This time I decided to use VMs rather than my working machine so that interruptions were minimised.

I created a new Windows 10 VM and as normal for VMs I set the initial memory to 512MB and used dynamic memory so that the machine could claim more RAM if required. Windows 10 installed with no problems. (Remember this).

I then went into Window Update and signed into the Windows Insider program. After triggering a scan fro updates build 16241 showed up and started downloading. Great.

It tried to install but failed with a message that 2GB of RAM was needed to perform the install!

So I can install from scratch with less than 2GB of RAM but I can’t update the build unless I have 2GB RAM.

Nice piece of joined up thinking there guys.

Sometimes you just have to laugh.

Posted in Windows 10 | 2 Comments

Unblock and rename files

I have a bunch of files that I’ve downloaded to a specific folder. I need to unblock and rename files in that folder. The rename involves replacing a plus sign with a space.

$path = 'C:\Users\Richard\Downloads\Walks'

$files = Get-ChildItem -Path $path -File

foreach ($file in $files) { 
   Unblock-File -Path $file.FullName
   $newname = $file.FullName -replace '\+', ' '
   Rename-Item -Path $file.FullName -NewName $newname

 }

Get-ChildItem -Path $path

I initially tried using a single pipeline but Unblock-File doesn’t generate any output which also blocks the pipeline – oops.

Read the list of files into an array. Iterate over the array and Unblock each file. Then rename the file. To use the –replace operator you need to escape the plus sign.

Display the files post rename as a check.

Posted in Powershell | Leave a comment

Change a computer’s description

The Win32_OperatingSystem class exposes the machines Description. This is how you can easily change a computer’s description.

PS> Get-CimInstance -ClassName Win32_OperatingSystem | select Description

Description
-----------




PS> Get-CimInstance -ClassName Win32_OperatingSystem | Set-CimInstance -Property @{Description = 'Richards Laptop'}

PS> Get-CimInstance -ClassName Win32_OperatingSystem | select Description

Description
-----------
Richards Laptop

You can see that the description is originally blank. Get the CimInstance of Win32_OperatingSystem and pipe it to Set-CimInstance. The property to change and its new value are held in the hash table that’s the value given to the –Property parameter. You can modify multiple properties at once – just add them as property-value pairs to the hash table

Posted in PowerShell and CIM | Leave a comment

Control split output

In this post I’ll show you show to control split output – that is control the number of strings that are returned.

If you use –split with just a delimiter you’ll get a split occurring at every occurrence of the delimiter:

PS> 'SundayJanuary 01 Jan 1 New Years Day First Monday if 1st is Saturday or Sunday' -split ' '

SundayJanuary
01
Jan
1
New
Years
Day
First
Monday
if
1st
is
Saturday
or
Sunday

But we want the holiday information to be in a single string. Rather than spending effort putting it back together you can control the number of strings that are output:

PS> 'SundayJanuary 01 Jan 1 New Years Day First Monday if 1st is Saturday or Sunday' -split ' ',5

SundayJanuary
01
Jan
1
New Years Day First Monday if 1st is Saturday or Sunday

In this case we’ve said we want 5 strings returned so everything after the 4th split is returned as a single string.

This makes our coding easier and neater

$uri = "http://www.officeholidays.com/countries/united_kingdom/index.php"

 $html = Invoke-WebRequest -Uri $uri

 $holidays = ($html.ParsedHtml.getElementsByTagName("table") | 

 where ClassName -eq 'list-table' | 

 select -ExpandProperty InnerText) -split "`n"

$holidays.Count

$hols = foreach ($holiday in $holidays[1..($holidays.Count -1)]){
   $x = $holiday -split ' ',5
   $y = $x[0] -split "day"
   
   $props = [ordered]@{
     DayOfWeek = "$($y[0])day"
     Day = $x[1]
     Month = $y[1]
     Holiday = $x[4]
   }
   
   New-Object -TypeName PSObject -Property $props
   

 }

$hols | Format-Table -AutoSize -Wrap

When I wrote this:

https://richardspowershellblog.wordpress.com/2017/07/09/office-holidays/

I said that the string handling was ugly and there must be a better way – I remembered!

Posted in Powershell | Leave a comment