Skip to main content
TopMiniSite

Back to all posts

How to Add Durations In Powershell?

Published on
3 min read
How to Add Durations In Powershell? image

Best PowerShell Tools to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms

BUY & SAVE
$47.49 $49.99
Save 5%
PowerShell for Penetration Testing: Explore the capabilities of PowerShell for pentesters across multiple platforms
4 Learn PowerShell Scripting in a Month of Lunches

Learn PowerShell Scripting in a Month of Lunches

BUY & SAVE
$50.21
Learn PowerShell Scripting in a Month of Lunches
5 Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts

BUY & SAVE
$27.00 $59.99
Save 55%
Troubleshooting SharePoint: The Complete Guide to Tools, Best Practices, PowerShell One-Liners, and Scripts
6 AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

BUY & SAVE
$48.99
AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease
7 Learn PowerShell Toolmaking in a Month of Lunches

Learn PowerShell Toolmaking in a Month of Lunches

BUY & SAVE
$20.52 $44.99
Save 54%
Learn PowerShell Toolmaking in a Month of Lunches
8 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
+
ONE MORE?

To add durations in PowerShell, you can use the Add() method available on the TimeSpan object. You can create two TimeSpan objects representing the durations you want to add, and then use the Add() method to add them together. Here's an example:

$duration1 = New-TimeSpan -Days 1 -Hours 3 -Minutes 30 $duration2 = New-TimeSpan -Hours 4 -Minutes 45

$result = $duration1.Add($duration2)

Write-Output $result

In this example, $duration1 represents a duration of 1 day, 3 hours, and 30 minutes, and $duration2 represents a duration of 4 hours and 45 minutes. When you add them together using the Add() method, the result will be a new TimeSpan object representing the combined duration.

How to add months to a specific date in PowerShell?

To add months to a specific date in PowerShell, you can use the AddMonths method from the DateTime object. Here's an example:

# Define the specific date $date = Get-Date "2022-01-15"

Add 3 months to the specific date

$newDate = $date.AddMonths(3)

Display the new date

$newDate

In this example, we first define the specific date as January 15, 2022. Then, we use the AddMonths method to add 3 months to the specific date. Finally, we display the new date which will be April 15, 2022. You can adjust the number of months to add as needed.

How to calculate total time from multiple durations in PowerShell?

To calculate the total time from multiple durations in PowerShell, you can use the following steps:

  1. Create an array of durations that you want to calculate the total time for.
  2. Loop through each duration in the array and convert it to a TimeSpan object using the New-TimeSpan cmdlet.
  3. Sum up all the TimeSpan objects to get the total time.
  4. Display the total time in the desired format.

Here is an example code snippet that demonstrates how to calculate the total time from multiple durations in PowerShell:

$durations = @("2:30:00", "1:45:00", "0:45:00") $totalTime = New-TimeSpan

foreach ($duration in $durations) { $timeSpan = New-TimeSpan -Hours $($duration -split ':')[0] -Minutes $($duration -split ':')[1] -Seconds $($duration -split ':')[2] $totalTime = $totalTime.Add($timeSpan) }

Write-Output "Total time: $($totalTime.ToString('hh\:mm\:ss'))"

In this example, we have an array $durations that contains three durations in the format "hours:minutes:seconds". We use a foreach loop to iterate through each duration, convert it to a TimeSpan object, and add it to the totalTime variable. Finally, we display the total time in the "hh:mm:ss" format using the ToString method.

How to add hours to a duration in PowerShell?

To add hours to a duration in PowerShell, you can use the AddHours() method from the System.TimeSpan class. Here is an example:

# Create a TimeSpan object representing a duration $duration = New-TimeSpan -Hours 2 -Minutes 30

Add 1 hour to the duration

$duration = $duration.AddHours(1)

Display the updated duration

Write-Output $duration

In this example, we first create a TimeSpan object representing a duration of 2 hours and 30 minutes. We then use the AddHours() method to add 1 hour to the duration. Finally, we display the updated duration using the Write-Output cmdlet.