ImportExcel icon indicating copy to clipboard operation
ImportExcel copied to clipboard

Export-Excel doesn't export dates correctly

Open marinkovicpetar opened this issue 10 years ago • 11 comments

I am playing around with some AD audit scripts, for internal use, and one of them is to find all enabled users inside a specific OU and export them to Excel. List should be sorted with WhenCreated user property.

for example, Get-ADUser -Filter {(Enabled -eq $true) -and (PasswordNeverExpires -eq $false)} -Properties * | Select-Object SamAccountName, DisplayName, Department, Office, Company, @{l="WhenCreated";e={$.WhenCreated.ToShortDateString()}}, @{l="PasswordLastSet";e={$.PasswordLastSet.ToShortDateString()}} | Sort-Object -Property {$_.WhenCreated -as [datetime] }

This one gives me the dataset I want, with users sorted by their creation date. When I export this to CSV, open the CSV in Excel, and then save it as .xlsx file, WhenCreated column is correctly recognised and I can use Filter on it to sort it from the beginning or the end. But, when I use Export-Excel with that CSV, Excel reports an error for that column and offers me to fix the error by selecting 2 year format with 19XX or 20XX. If I click on ignore error, it's fine, but would like for my users to fix it so they can use Filter function right away.

WhenCreated property returns both date and time, so that's why I use .ToShortDateString() function

I understand this is not a major bug or an issue, but something that I would like to fix for myself, so any help is appreciated. I also tried to set up date in powershell in different ways, also by ToString("dd/MM/yyyy") but also no luck.

marinkovicpetar avatar Sep 05 '16 13:09 marinkovicpetar

Could you post a sample csv file, and I can see if I can get more clues about the issue.

dfinke avatar Sep 05 '16 14:09 dfinke

Sure, here it is

"SamAccountName","DisplayName","WhenCreated","PasswordLastSet"
"user1","Username1","04-Aug-08","26-Mar-16"
"user2","Username2","04-Nov-08","28-Jul-16"
"user3","Username3","15-Jan-09","17-May-16"
"user4","Username4","30-Mar-09","23-May-16"
"user5","Username5","15-Apr-09","12-Jul-16"
"user6","Username6","20-Aug-09","15-Jun-16"
"user7","Username7","20-Aug-09","23-May-16"
"user8","Username8","01-Sep-09","28-Aug-16"
"user9","Username9","01-Sep-09","08-Aug-16"

I've changed the original data, as you can assume.

Last two columns come from WhenCreated and PasswordLastSet properties of AD user

marinkovicpetar avatar Sep 05 '16 15:09 marinkovicpetar

Thanks. I'm wondering if it might be a culture setting.

dfinke avatar Sep 05 '16 15:09 dfinke

Sorry, forgot the screenshots to describe my situation better.

excel01

This is what Excel tries to "help" me with my errors

excel02

and the field is not a 'date' or 'custom' type, but 'general'

marinkovicpetar avatar Sep 05 '16 15:09 marinkovicpetar

You might need to convert the date when you use ToShortDateString.

Or maybe your culture setting is creating that format.

I saved your example to test.csv and the below worked.

rm .\date.xlsx -ErrorAction Ignore
Import-Csv test.csv | 
    % {

    $_.WhenCreated = (Get-Date($_.WhenCreated)).ToShortDateString()
    $_.PasswordLastSet = (Get-Date($_.PasswordLastSet )).ToShortDateString()
    $_
} | Export-Excel c:\temp\date.xlsx -show -AutoSize

dfinke avatar Sep 05 '16 18:09 dfinke

Nah, thanks for your help, but it doesn't work, and I guess it's some culture setting (before you mentioned it, wasn't aware that it has an impact to this). My AD script is doing the same, and even your example is giving me the same thing

excel03

Sorry for bugging you more, do you have idea what's the culture setting that can cause issues? Here is my output of Get-Culture

LCID             Name             DisplayName          
----             ----             -----------                                                                                                                   
1033             en-US            English (United States)  

And really, thanks a lot for helping out, as well as for a great module!

marinkovicpetar avatar Sep 05 '16 18:09 marinkovicpetar

Hmm, definite mystery. What do you get from this?

(get-date("04-Aug-08")).ToShortDateString()

dfinke avatar Sep 05 '16 20:09 dfinke

04-Aug-08 on both systems (Windows 7 Pro and Windows 8.1 Enterprise) with RSAT installed and Powershell 4. On both, my date settings are dd-MMM-yyyy but local settings are US. Also, both installs are untouched US International ISO

marinkovicpetar avatar Sep 05 '16 21:09 marinkovicpetar

How about? (get-date("04-Aug-08")).ToString("MM/dd/yyyy")

dfinke avatar Sep 07 '16 21:09 dfinke

It's the same, still treating the cell with "General" type. I wouldn't want to bug you anymore, I can mark this one as solved, since it seems it's an issue with my setup.

Thanks once again!

marinkovicpetar avatar Sep 12 '16 15:09 marinkovicpetar

Interesting. If you find the answer, please post.

dfinke avatar Sep 12 '16 18:09 dfinke