PowerShell – Start getting your Computer Last BootUp Time

The PowerShell community has done an Excellent job in providing LOTs of sample codes. But sometime can be very intimidating for a beginner to understand. Well, PowerShell can be both simple, and complicated but not impossible to learn.

Is good to know there are other ways other to get the same or similar result. And I’m not against it because you can even integrate those solutions into PowerShell.

BUT, you need to realized this is a SKILL you need nowadays. PowerShell can’t be ignore any more. If you don’t build up this skill then you’re probably going to lose a job opportunity.

Now, here’s a series of oneliners to assist you in getting the computer BootUp time information:

## - Simple way to start knowing your PSobject
$x = get-wmiObject -Class Win32_OperatingSystem -Computername "YourComputerName";

## - Discovering you PSobject content:
$x | get-member;

## - Sample getting the PSObject 'Property' names for computername and Lastboottime:
$x | Select csname, LastBootUpTime;

## - Need to use the "ConvertToDateTime" script method which is in your PSobject $x:
$x.ConvertToDateTime($x.LastBootUpTime);

## - Wee need to create a one-liner to display the date/time correctly
## - using scriptblock expression:
$x | Select csname, `
   @{label='MyLastBootUpTime';Expression={$_.ConvertToDateTime($_.LastBootUpTime)};} `
   | format-table -autosize;

## - Now, pay attention to the Get-member results and you'll find the following methods:
#  Reboot()
#  SetDateTime()
#  Shutdown()
#  Win32Shutdown()
#  Win32ShutdownTracker()
## - You will realized the PSObject you just created can be use to perform
## - task such as: reboot or shutdown.

## - This is an example to create an Array list of servernames:
$srvList = @("Server1","Server2","Server3");

###### BUILDING A SCRIPT with above onliners ########
## - Now we need to loop through each of the servers in the array with 'Foreach'
## - and display the results on screen:

$srvList = @("Server1","Server2","Server3");

foreach($server in $Srvlist)
{
 $x = get-wmiObject -Class Win32_OperatingSystem -Computername $server;
 $x | Select csname, `
   @{label='MyLastBootUpTime';Expression={$_.ConvertToDateTime($_.LastBootUpTime)};} `
   | format-table -autosize;
};

## OR, the next sample will create a PSObject from you looping results:

$srvList = @("Server1","Server2","Server3");

[array] $myUpTimeList = foreach($server in $Srvlist)
{
 $x = get-wmiObject -Class Win32_OperatingSystem -Computername $server;
 $x | Select csname, `
   @{label='MyLastBootUpTime';Expression={$_.ConvertToDateTime($_.LastBootUpTime)};};
};

$myUpTimelist | ft -auto;

## - End of scripts

This is your starting code block. This code can be improved to suite your need by adding more PSobject properties and/or routing the results to an output file.

I hope this can give some insight on how PowerShell can help in your admin tasks.

This entry was posted in PowerShell, Windows 2012, Windows 8. Bookmark the permalink.