Hi folks
I can not get a windows installation date from script
The script:
$Computers = Get-Content -Path C:powershellscriptsComputers.txt
$AllComputers = @()
foreach($machine in $Computers)
{
$objCompSys = Get-WmIObject -class 'Win32_ComputerSystem' -cn $machine -EA silentlyContinue -namespace 'rootCIMV2'
foreach($item in $objCompSys)
{
$compname = $item.Name
$physicalRAM = [Math]::Ceiling($item.TotalPhysicalMemory / 1GB)
$CurrentUser = $Item.username
}
$objPhydrive = Get-WmIObject -class 'Win32_DiskDrive' -cn $machine -EA silentlyContinue -namespace 'rootCIMV2'
foreach($item in $objPhydrive)
{
$PhyDrive = $item.size
}
$objCompOS = Get-WmIObject -class 'Win32_OperatingSystem' -cn $machine -EA silentlyContinue -namespace 'rootCIMV2'
foreach($item in $objCompOS)
{
$WindowsVersion = $Item.Caption
$ServicePack = $Item.csdversion
}
#$DiskItems = gwmi Win32_LogicalDisk -filter 'DriveType=3' -Comp $machine
$DiskItems = Get-WMIObject -Class Win32_DiskDrive -Filter {DeviceId = '.PHYSICALDRIVE0'} | select -ExpandProperty size
foreach($item in $DiskItems) {
}
$objProc = Get-WMIObject -class 'Win32_Processor' -cn $machine
foreach($item in $objProc)
{
$procType = $item.Name
}
$Object = New-Object PSObject -Property @{
CompName = $compname
WindowsVersion = $WindowsVersion
ServicePack = $ServicePack
WinInsDate = $WinInsDate
CurrentUser = $CurrentUser
}
$AllComputers += $Object
$AllComputers | select CompName,WindowsVersion,ServicePack,WinInsDate | Out-GridView
I can not get a windows installation date from script
The script:
$Computers = Get-Content -Path C:powershellscriptsComputers.txt
Nov 22, 2011 - The System.Management.ManagementDateTimeConverter class was made to solve your problem. Use its ToDateTime() method. It properly parses milliseconds. The Win32_OperatingSystem class represents an operating system installed on a Win32 computer system. Any operating system that can be installed on a Win32 system is a descendent (or member) of this class. Example: Microsoft Windows 95.
$AllComputers = @()
foreach($machine in $Computers)
{
$objCompSys = Get-WmIObject -class 'Win32_ComputerSystem' -cn $machine -EA silentlyContinue -namespace 'rootCIMV2'
foreach($item in $objCompSys)
{
$compname = $item.Name
$physicalRAM = [Math]::Ceiling($item.TotalPhysicalMemory / 1GB)
$CurrentUser = $Item.username
}
$objPhydrive = Get-WmIObject -class 'Win32_DiskDrive' -cn $machine -EA silentlyContinue -namespace 'rootCIMV2'
foreach($item in $objPhydrive)
{
$PhyDrive = $item.size
}
$objCompOS = Get-WmIObject -class 'Win32_OperatingSystem' -cn $machine -EA silentlyContinue -namespace 'rootCIMV2'
foreach($item in $objCompOS)
{
$WindowsVersion = $Item.Caption
$ServicePack = $Item.csdversion
$WinInsDate = ([WMI] ').ConvertToDateTime((Get-WmiObject Win32_OperatingSystem -ComputerName $item.InstallDate)) | Get-Date -Format d
}
#$DiskItems = gwmi Win32_LogicalDisk -filter 'DriveType=3' -Comp $machine
$DiskItems = Get-WMIObject -Class Win32_DiskDrive -Filter {DeviceId = '.PHYSICALDRIVE0'} | select -ExpandProperty size
foreach($item in $DiskItems) {
$DiskSize = $Item.Size/1GB
}
$objProc = Get-WMIObject -class 'Win32_Processor' -cn $machine
foreach($item in $objProc)
{
$procType = $item.Name
}
$Object = New-Object PSObject -Property @{
CompName = $compname
WindowsVersion = $WindowsVersion
ServicePack = $ServicePack
WinInsDate = $WinInsDate
CurrentUser = $CurrentUser
}
$AllComputers += $Object
$AllComputers | select CompName,WindowsVersion,ServicePack,WinInsDate | Out-GridView
Tip: You can convert WMI date (format DTMF Distributed Management Task Force) to DateTime:
2 4 6 | $installDate=(Get-WmiObject-ClassWin32_OperatingSystem).InstallDate # Solution 1 [Management.ManagementDateTimeConverter]::ToDateTime($installDate) # Solution 2 |
Note: Use CIM cmdlets (available since PowerShell v3) instead of WMI cmdlets, moreover CIM return a more understandable datetime format :
(Get-CimInstance-ClassNameWin32_OperatingSystem).InstallDate
(Get-CimInstance-ClassNameWin32_OperatingSystem).InstallDate
MSDN:
ManagementDateTimeConverter.ToDateTime Method
ManagementDateTimeConverter.ToDateTime Method