Powershell script to show system uptime

Ian

Administrator
Joined
Feb 23, 2002
Messages
19,873
Reaction score
1,499
I am trying to generate a powershell script that shows system uptime on a Window 11 PC, can you help please?
 

AI Answer Bot

ChatGPT Bot
Joined
Mar 14, 2023
Messages
15
Reaction score
0
Yes, I can definitely help you! Here is a simple PowerShell script that you can use to get the system uptime:

Code:
$OperatingSystem = Get-CimInstance -ClassName Win32_OperatingSystem
$LastBootUpTime = $OperatingSystem.LastBootUpTime
$CurrentTime = Get-Date
$Uptime = $CurrentTime - $LastBootUpTime
Write-Host "System Uptime: $($Uptime.Days) days, $($Uptime.Hours) hours, $($Uptime.Minutes) minutes, $($Uptime.Seconds) seconds"

This script does the following:
1. It uses the `Get-CimInstance` cmdlet to retrieve the system's LastBootUpTime property from the `Win32_OperatingSystem` class.
2. It retrieves the current time and calculates the difference between the current time and the LastBootUpTime property using the `-` operator.
3. Finally, it displays the system uptime in days, hours, minutes, and seconds using the `Write-Host` cmdlet.

Save the script with a .ps1 extension, and run it with elevated privileges (i.e., Run as Administrator) to ensure that you have the necessary permissions to retrieve this information.

I hope this helps! Let me know if you have any other questions.
 

Ian

Administrator
Joined
Feb 23, 2002
Messages
19,873
Reaction score
1,499
I hope this helps! Let me know if you have any other questions.
That's great, thanks. Can you please modify the script so that it logs to a file called uptime.log, which includes the uptime and uptime start time/date?
 

AI Answer Bot

ChatGPT Bot
Joined
Mar 14, 2023
Messages
15
Reaction score
0
That's great, thanks. Can you please modify the script so that it logs to a file called uptime.log, which includes the uptime and uptime start time/date?
Yes, I can definitely help with that. Here's the modified script that logs the uptime and start time/date to a file called `uptime.log`:

Code:
$OperatingSystem = Get-WmiObject -Class Win32_OperatingSystem
$LastBootUpTime = $OperatingSystem.LastBootUpTime
$CurrentTime = Get-Date
$Uptime = $CurrentTime - $LastBootUpTime
$UptimeDays = $Uptime.Days
$UptimeHours = $Uptime.Hours
$UptimeMins = $Uptime.Minutes
$UptimeSecs = $Uptime.Seconds
$StartTime = $CurrentTime - $Uptime

Write-Host "System Uptime: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds"

$Path = "C:\Logs\uptime.log"
$Content = "System Uptime: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds" + "`r`n" + "Start Time/Date: $StartTime"

Add-Content $Path $Content

This script does the following:
1. It uses the `Get-WmiObject` cmdlet to retrieve the system's `LastBootUpTime` property from the `Win32_OperatingSystem` class.
2. It retrieves the current time and calculates the difference between the current time and the `LastBootUpTime` property using the `-` operator.
3. It formats the uptime values as days, hours, minutes, and seconds and sets them to variables.
4. It calculates the start time/date by subtracting the uptime from the current time.
5. It displays the system uptime in days, hours, minutes, and seconds using the `Write-Host` cmdlet.
6. It sets the log file path and content.
7. It appends the uptime and start time/date to the log file using the `Add-Content` cmdlet.

Make sure to replace `C:\Logs\uptime.log` with the desired file path for the log file.

I hope this helps! Let me know if you have any other questions.
 
Joined
May 2, 2008
Messages
1
Reaction score
0
Yes, I can definitely help with that. Here's the modified script that logs the uptime and start time/date to a file called uptime.log:

Code:
$OperatingSystem = Get-WmiObject -Class Win32_OperatingSystem
$LastBootUpTime = $OperatingSystem.LastBootUpTime
$CurrentTime = Get-Date
$Uptime = $CurrentTime - $LastBootUpTime
$UptimeDays = $Uptime.Days
$UptimeHours = $Uptime.Hours
$UptimeMins = $Uptime.Minutes
$UptimeSecs = $Uptime.Seconds
$StartTime = $CurrentTime - $Uptime

Write-Host "System Uptime: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds"

$Path = "C:\Logs\uptime.log"
$Content = "System Uptime: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds" + "`r`n" + "Start Time/Date: $StartTime"

Add-Content $Path $Content

This script does the following:
1. It uses the Get-WmiObject cmdlet to retrieve the system's LastBootUpTime property from the Win32_OperatingSystem class.
2. It retrieves the current time and calculates the difference between the current time and the LastBootUpTime property using the - operator.
3. It formats the uptime values as days, hours, minutes, and seconds and sets them to variables.
4. It calculates the start time/date by subtracting the uptime from the current time.
5. It displays the system uptime in days, hours, minutes, and seconds using the Write-Host cmdlet.
6. It sets the log file path and content.
7. It appends the uptime and start time/date to the log file using the Add-Content cmdlet.

Make sure to replace C:\Logs\uptime.log with the desired file path for the log file.

I hope this helps! Let me know if you have any other questions.
Please modify to capture an entire domain.
 

AI Answer Bot

ChatGPT Bot
Joined
Mar 14, 2023
Messages
15
Reaction score
0
Please modify to capture an entire domain.
If you want to capture the uptime for all the computers in a domain, you'll need to modify the original script to loop through all the computers in the domain and retrieve their individual uptime. Here's an updated script that can accomplish that:

Code:
$domain = "yourdomain.com" # Replace with your domain name
$cred = Get-Credential # Enter Domain Admin credentials

$computerList = Get-ADComputer -Filter * -Credential $cred -Server $domain | Select-Object -ExpandProperty Name

foreach ($computer in $computerList) {
    $OperatingSystem = Get-WmiObject -ComputerName $computer -Credential $cred -Class Win32_OperatingSystem
    $LastBootUpTime = $OperatingSystem.LastBootUpTime
    $CurrentTime = Get-Date
    $Uptime = $CurrentTime - $LastBootUpTime
    $UptimeDays = $Uptime.Days
    $UptimeHours = $Uptime.Hours
    $UptimeMins = $Uptime.Minutes
    $UptimeSecs = $Uptime.Seconds
    $StartTime = $CurrentTime - $Uptime

    Write-Host "System Uptime for $computer: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds"

    $Path = "C:\Logs\uptime.log"
    $Content = "Computer Name: $computer" + "`r`n" + "System Uptime: $UptimeDays days, $UptimeHours hours, $UptimeMins minutes, $UptimeSecs seconds" + "`r`n" + "Start Time/Date: $StartTime"

    Add-Content $Path $Content
}

This script does the following:
1. It specifies the domain name and domain admin credentials using the Get-Credential cmdlet.
2. It uses the Get-ADComputer cmdlet to retrieve a list of all computers in the domain and selects their names.
3. It loops through each computer in the list.
4. It retrieves the system's LastBootUpTime property from the Win32_OperatingSystem class.
5. It retrieves the current time and calculates the difference between the current time and the LastBootUpTime property using the - operator.
6. It formats the uptime values as days, hours, minutes, and seconds and sets them to variables.
7. It calculates the start time/date by subtracting the uptime from the current time.
8. It displays the system uptime for each computer using the Write-Host cmdlet.
9. It sets the log file path and content, including the computer name.
10. It appends the computer name, uptime, and start time/date to the log file for each computer in the list using the Add-Content cmdlet.

Make sure to replace yourdomain.com with the appropriate domain name and provide the appropriate credentials. Also, replace C:\Logs\uptime.log with the desired file path for the log file.

I hope this helps! Let me know if you have any other questions.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top