Author Posts

September 10, 2018 at 12:29 pm

Hi!

I'm pretty new to Powershell but I already noticed that it has made my work faster and easier to find some simple information. Right now i'm figuring out how to combine result from Get-Aduser and Net user together into one Format List.

The reason I want this is because the net user command gives me a lot of information I don't need. Except for the PasswordExpirationDate. With Get-ADUser, I can't get that specific information.

This is what I got so far.

Get-ADUser username -Properties | fl DisplayName, SamAccountName, LastLogonDate, PasswordExpired, PasswordExpires, PasswordLastSet, AccountExpirationDate, Mobile, telephoneNumber, extensionAttribute1

The command is working fine, but I can't find the solution to add the ExpirationDate of the password into that format list.

Could someone help me? Any improvement suggestions are also welcome, just trying to learn the language 🙂

September 10, 2018 at 1:08 pm

You have a couple of small problems, first there is no 'standard' property "PasswordExpires", it's "PasswordNeverExpires". It's best to define the properties you want returned from Active Directory, like so:

Get-ADUser -Identity USERNAME -Properties DisplayName, SamAccountName, LastLogonDate, PasswordExpired, PasswordNeverExpires, PasswordLastSet, AccountExpirationDate, Mobile, telephoneNumber, extensionAttribute1

Be careful with Format-* commands because they stop the pipeline. You cannot do Get-AdUser ... | Format-List ... | Export-CSV ...

There are some free e-books above including Powershell Gotchas that are great beginner resources

September 10, 2018 at 1:08 pm

Great, PowerShell makes our life easier and it will surprise you in each and every moment with its capabilities on the go.

Here you are using two APIs

  • PowerShell cmdlet (Get-ADUser)
  • net.exe (native executable)

PowerShell cmdlet returns objects and net use returns text and you need a combined result.
At a high level, you can use PSCustomObjects and in many other ways. Here I'll show another method using calculate properties.

Get-ADUser -Identity username -properties * | Select-Object -Property DisplayName, LastLogonDate, PasswordExpired, PasswordLastSet, AccountExpirationDate, Mobile, telephoneNumber, extensionAttribute1, @{E={((net user username /domain|Select-String -Pattern 'Password expires') -split 'Password expires')[-1].Trim() };L='PasswordExpires'}

For learning PowerShell, You can always start with
https://www.manning.com/books/learn-windows-powershell-in-a-month-of-lunches-second-edition
https://mva.microsoft.com/liveevents/powershell-jumpstart

September 10, 2018 at 1:44 pm

Thank you both for your replies. I got it working with the solution kvprasoon gave me, but I learned from both of you!

The PSCostomObject is a little hard, but I will read the article.

Thank you both again!