Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7824653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T08:40:19+00:00 2026-06-02T08:40:19+00:00

I have a small script which retrieves the LastLogonTimestamp and the SAMAccount for all

  • 0

I have a small script which retrieves the LastLogonTimestamp and the SAMAccount for all users in a particular OU in AD and converts the timestamp to a date and extracts just the date from the string. That part works fine. I then would like to output that to a CSV so it may be opened in Excel and be perfectly formated into columns and look all pretty.

I have tried ConvertTo-Csv and Export-Csv but have been uncuccessful. The problem is I am new to Powershell. This is my first script and I don’t fully understand how this works. My script is probably terribly messy and illogical but it does the job so far.

Please help. Thanks.

$userlist = Get-ADUser -SearchBase "OU=IT,DC=whatever,DC=com,DC=au" -Filter * -Properties * | Select-Object -Property Name,LastLogonTimestamp,SAMAccountName | Sort-Object -Property Name

$userlist | ForEach-Object {
    $last = $_.LastLogonTimestamp;
    $ADName = $_.SAMAccountName;

    $tstamp = w32tm /ntte $last;
    if($tstamp.Length -lt "40"){}else
    {
        $ADDate = [DateTime]::Parse($tstamp.Split('-')[1]).ToString("dd/MM/yyyy")
        write-host $ADDate;
        write-host $ADName;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T08:40:22+00:00Added an answer on June 2, 2026 at 8:40 am

    You will have to create objects for each user and pipe those to the Export-CSV cmdlet:

    $usersList | %{
    
    # current logic
    
    $user = new-object psobject
    $user | add-member -membertype noteproperty -name LastLogon -value $last
    $user | add-member -membertype noteproperty -name ADName -value $ADName
    $user | add-member -membertype noteproperty -name ADDate -value $ADDate
    $user
    } | export-csv test.csv -notype
    

    Alternative syntax for populating the object:

    $properties = @{"LastLogon" = $last; "ADName" = $ADName; "ADDate" = $ADDate}
    $user = new-object psobject -property $properties
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small script which im using to test PHP mail(), as below:
I have a small ruby script in which I'd like to use ActiveRecord to
I have a small script, I have it adding up all of the number
I have a small php script which allows me to upload image file on
I have written a script which is basically a small wysiwyg signature generator for
I have small PHP script which has $query = SELECT MAX(id) FROM `dbs`; //query
I have a small python script which serves as a configuration file. The structure
I have written a small bash script which needs an ssh tunnel to draw
I have a small perl script ( found here ) which adds command line
I have a small script, which is called daily by crontab using the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.