Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

Active Directory Powershell: Quick tip LastLogonTimeStamp and pwdLastSet

$
0
0

 

Here is a quick tip on how to quickly convert properties like LastLogonTimeStamp and pwdLastSet into readable results in your PowerShell Script.

Here is the problem, when running commands like get-aduser or get-adcomputer, results of fields are unreadable and require additional formatting in order to read.

Example:

get-aduser chad -properties lastlogontimestamp,pwdLastSet | select samaccountname, lastlogontimestamp,pwdLastSet

image

There are several blogs on how to use a calculated property to convert and display the results, that is not the purpose of this blog entry. For me I usually forget how to do this (older I get the less I remember) and have to review previous scripts on how to do this each time I write one.  I also usually write bigger scripts were I have to perform this same conversion multiple times, which causes my script to grow in size and makes it a little more difficult to read.

This is how most articles and blogs do this conversion.

Example:

get-aduser chad -properties lastlogontimestamp,pwdLastSet | select samaccountname, `
    @{Name="LastLogonTimeStamp";Expression={([datetime]::FromFileTime($_.LastLogonTimeStamp))}},`
    @{Name="pwdLastSet";Expression={([datetime]::FromFileTime($_.pwdLastSet))}}

image

Now keep in mind copying and pasting this over and over isn't that hard.  But I’m going to provide a way to define it once in a script and then just reference it as needed. In additional going to provide a way to add this to PowerShell ISE as a snippet so that it can be added in a script as needed.

Here is the solution, store the calculated property hash into a variable, reference the variable instead of the entire hash.

Example:

$hash_lastLogonTimestamp = @{Name="LastLogonTimeStamp";Expression={([datetime]::FromFileTime($_.LastLogonTimeStamp))}}
$hash_pwdLastSet = @{Name="pwdLastSet";Expression={([datetime]::FromFileTime($_.pwdLastSet))}}
 
get-aduser chad -properties lastlogontimestamp,pwdLastSet | `
    select samaccountname, $hash_lastLogonTimestamp,$hash_pwdLastSet

image

Using the variable I can define this once and reuse it through out a script or the PowerShell cli

Example:

$hash_lastLogonTimestamp = @{Name="LastLogonTimeStamp";Expression={([datetime]::FromFileTime($_.LastLogonTimeStamp))}}
$hash_pwdLastSet = @{Name="pwdLastSet";Expression={([datetime]::FromFileTime($_.pwdLastSet))}}
 
get-aduser chad -properties lastlogontimestamp,pwdLastSet | `
    select samaccountname, $hash_lastLogonTimestamp,$hash_pwdLastSet
get-adcomputer corp-apps -properties lastlogontimestamp,pwdLastSet | `
    select samaccountname, $hash_lastLogonTimestamp,$hash_pwdLastSet
get-aduser ryan -properties lastlogontimestamp,pwdLastSet | `
    select samaccountname, $hash_lastLogonTimestamp,$hash_pwdLastSet
get-adcomputer corp-web01 -properties lastlogontimestamp,pwdLastSet | `
    select samaccountname, $hash_lastLogonTimestamp,$hash_pwdLastSet

image

For those that use PowerShell ISE,  make this something to easily reference by using ise snippets.  More Information

Here is that cmdlets to run in ISE to make this a snippet:

New-IseSnippet -Description "Convert Active Directory property LastLogonTimestamp" `
    -Text '$hash_lastLogonTimestamp = @{Name="LastLogonTimeStamp";Expression={([datetime]::FromFileTime($_.LastLogonTimeStamp))}}' `
    -Title "Convert LastLogonTimeStamp" -Author "Chad"
 
New-IseSnippet -Description "Convert Active Directory property pwdLastSet" `
    -Text '$hash_pwdLastSet = @{Name="pwdLastSet";Expression={([datetime]::FromFileTime($_.pwdLastSet))}}' `
    -Title "Convert pwdLastSet" -Author "Chad"

 

In Powershell ISE, put the cursor on the line where the snippet needs to go, Press Ctrl + j and then select the newly created snippet.

image

Just like that no longer do I have to look through previous scripts.

That is all I have for now, Hope you find this useful.

-Chad


Viewing all articles
Browse latest Browse all 34890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>