Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script for exporting NTUSER.DATfile modified date (for all users) on a share to excel 2

Status
Not open for further replies.

win2kwire

Technical User
Dec 14, 2005
35
ES
Hi,

I wonder if anyone can help me with the script (below).
This is for checking and exporting the NTUSER.DATfile "modified date" (for all users) on a share, to a spreadsheet.

The script runs without errors but exports modified date for all folders and files (for all users) to excel.
I only want the information for NTUSER.DAT

Any help on modifying the script to desired effect appreciated.
Thanks in advance

Script:
............................................
Option Explicit

Dim strFolder, objFSO, objFolder
Dim strExcelPath, objExcel, objSheet, intRow

Const xlExcel7 = 39

' Specify main folder.
strFolder = "D:\test"

' Retrieve folder object.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)

' Specify spreadsheet to be created.
strExcelPath = "d:\test\user.xls"

' Create workbook.
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Add

' Bind to worksheet.
Set objSheet = objExcel.ActiveWorkbook.Worksheets(1)

' Enumerate files.
intRow = 0
Call GetFiles(objFolder)

' Save spreadsheet, close workbook, and quit Excel.
objExcel.ActiveWorkbook.SaveAs strExcelPath, xlExcel7
objExcel.ActiveWorkbook.Close
objExcel.Application.Quit

' Alert user.
Wscript.Echo "Done"

Sub GetFiles(ByVal objParent)
' Enumerate files in folder.
' Variable intRow must have global scope.
Dim objFile, objChild

For Each objFile In objParent.Files
intRow = intRow + 1
objSheet.Cells(intRow, 1).Value = objFile.Path
objSheet.Cells(intRow, 2).Value = objFile.DateLastModified
Next

' Recurse through nested folders.
For Each objChild In objParent.SubFolders
Call GetFiles(objChild)
Next
End Sub
..............................................................
 
Try this for getting the desired info.

Code:
$ErrorActionPreference = "SilentlyContinue"
$Report = $Null
$Path = "C:\Users"
$UserFolders = $Path | GCI -Directory

ForEach ($UserFolder in $UserFolders)
{
$UserName = $UserFolder.Name
If (Test-Path "$Path\$UserName\NTUSer.dat")
    {
    $Dat = Get-Item "$Path\$UserName\NTUSer.dat" -force 
    $Dat = $Dat.LastWriteTime
    Write-Host $UserName $Dat
    $Report = $Report + "$UserName`t$Dat`r`n"
    $Dat = $Null
    }
Else
    {
    $Dat = "No file"
    $Report = $Report + "$UserName`t$Dat`r`n"
    }
}
$Report | Out-File c:\Temp\NTUserDatInfo.txt

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Hi Mark,

Thanks for your help.
copied script, edited it as below, changed to file.ps1, ran from powershell, runs without errors but output text file only contains: "No File"


$ErrorActionPreference = "SilentlyContinue"
$Report = $Null
$Path = "D:\test\Users"
$UserFolders = $Path | GCI -Directory

ForEach ($UserFolder in $UserFolders)
{
$UserName = $UserFolder.Name
If (Test-Path "$Path\$UserName\UPM_Profile\NTUSer.dat")
{
$Dat = Get-Item "$Path\$UserName\UPM_Profile\NTUSer.dat" -force
$Dat = $Dat.LastWriteTime
Write-Host $UserName $Dat
$Report = $Report + "$UserName`t$Dat`r`n"
$Dat = $Null
}
Else
{
$Dat = "No file"
$Report = $Report + "$UserName`t$Dat`r`n"
}
}
$Report | Out-File d:\test\NTUserDatInfo.txt
 
What version of PowerShell are you using. The LastWriteTime requires 3.0 or better.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Thanks Mark,
works fine with 4.0, appreciated.

Very grateful for your help and if you know how I can make the script look for ALL instances of NTuser.dat in all users profile directories and folders and report modified dates will be appreciated.

Regards
Ben
 
Ben, the code I provided should enumerate through all user folders (at the base level). NTUSER.DAT should only exist at the root of each users profile.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Probably should point out that the account the script is run under needs to be an admin account to have read access to the other user folders.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Appreciated Mark,
Many thanks for your help

Regards
Ben
 
If the code provided is helpful, you can click the Great Post, Star it! link to let others looking for similar help what helped you.

I hope that helps.

Regards,

Mark

No trees were harmed in posting this message, however a significant number of electrons were terribly inconvenienced.

Check out my scripting solutions at
Work SMARTER not HARDER.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top