# Numbers, Dates and Types

Every value in PowerShell has a type, such as a whole number, text or a date; \[int\], \[decimal\] and \[datetime\] turn text into the type you need, and subtracting dates, AddDays and Get-Date -Format answer questions such as how many days are left until a trip.

Apprentice · PowerShell · Updated November 10, 2026

## After this lesson you can

- Tell which type a value has, and turn text into a number or a date with \[int\], \[decimal\] and \[datetime\]
- Work out dates with \[datetime\] and Get-Date, such as the days between two dates, a date some days later, and a date in the format you need

## Key takeaways

- Every value has a type: 42 is a whole number (Int32) and '42' is text (String). `.GetType().Name` tells you which.
- With +, the left side decides: '42' + 8 joins text into 428, while 42 + '8' adds up to 50.
- A type's name in square brackets converts a value: [int]'42', [decimal]'4.50', [datetime]'2026-12-04'. Money goes in [decimal].
- One date minus another is a TimeSpan; its .Days is the number of days. .AddDays(30) moves a date, and -Format writes it your way.
- Write dates as year-month-day, such as 2026-12-04: every computer reads that the same way.

How many days until the trip? You could count squares on a calendar, and count them again tomorrow. Ada's letter in the workshop says she's back from the inventors' fair on December 4, and her apprentice wants to know how many days that leaves to get the machines running. PowerShell can count that for you, but only once it knows that `2026-12-04` is a date and not ten characters of text. The same goes for numbers: a price in a spreadsheet export is text until you say it's money. Knowing a value's **type**, and changing it, is what this lesson is about. An office manager counting days to a deadline and an IT tech checking how old a backup is do this every week.

## Every value has a type

A type is the kind of thing a value is: a whole number, text, a date. Every value can tell you its type:

```powershell
(42).GetType().Name
'42'.GetType().Name
(4.50).GetType().Name
(Get-Date).GetType().Name
```

```text
Int32
String
Double
DateTime
```

`Int32` is a whole number (an *integer*), `String` is text, `Double` is a number with a decimal point, and `DateTime` is a date with a time. The type matters the moment you add:

```powershell
'42' + 8
42 + '8'
```

```text
428
50
```

With `+`, the left side decides. Text on the left joins the 8 on as more text; a number on the left turns the `'8'` into a number and adds. That's how a total from a file, where every value is text, comes out as a long string of digits.

## Square brackets convert

Put a type's name in square brackets in front of a value, and PowerShell converts it, as *Saving Data: Export-Csv and Cleaning a CSV* did for prices. Read `[int]'42'` as "'42' as an int". The names are short: `[int]` for whole numbers, `[decimal]` for money, `[datetime]` for dates. They come from .NET, the toolkit PowerShell is built on.

```powershell
[int]'42' + 8
[int]'7.6'
[int]'seven'
```

```text
50
8
InvalidArgument: Cannot convert value "seven" to type "System.Int32". Error: "The input string 'seven' was not in a correct format."
```

`[int]` rounds to the nearest whole number, and text that isn't a number stops with an error. Why `[decimal]` for money? A `Double` stores most decimals as a very close guess, and the guesses can show:

```powershell
1.10 + 2.20 - 3.30
[decimal]1.10 + [decimal]2.20 - [decimal]3.30
```

```text
4.44089209850063E-16
0.0
```

The first answer is a tiny number (the `E-16` means sixteen places after the decimal point), not zero. `[decimal]` counts in exact cents.

A type in front of the variable's name locks the box to that type, so every value put in is converted, or refused:

```powershell
[int]$gearSize = 7
$gearSize = '21'
$gearSize + 1
$gearSize = 'seven'
```

```text
22
MetadataError: Cannot convert value "seven" to type "System.Int32". Error: "The input string 'seven' was not in a correct format."
```

## Dates: \`\[datetime\]\` and \`Get-Date\`

`[datetime]` turns text into a date. Write it year-month-day, the way every computer reads the same:

```powershell
[datetime]'2026-12-04'
```

```text
Friday, December 4, 2026 12:00:00 AM
```

Now the question from the opening. One date minus another is a **TimeSpan**, a length of time, and its `Days` property is the answer:

```powershell
[datetime]'2026-12-04' - [datetime]'2026-11-09'
([datetime]'2026-12-04' - [datetime]'2026-11-09').Days
```

```text
Days              : 25
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 0
Ticks             : 21600000000000
TotalDays         : 25
TotalHours        : 600
TotalMinutes      : 36000
TotalSeconds      : 2160000
TotalMilliseconds : 2160000000

25
```

To count from today, use `(Get-Date).Date`: today, at midnight, so the time of day doesn't cut a day off. `([datetime]'2026-12-04' - (Get-Date).Date).Days` gives the days left from the day you run it.

## Moving a date, and writing it your way

`AddDays` moves a date (a negative number goes back), `DayOfWeek` names the day, and `Get-Date -Format` writes a date in the shape you ask for. `yyyy` is the year, `MM` the month, `dd` the day, `dddd` the day's name and `MMMM` the month's:

```powershell
([datetime]'2026-11-07').AddDays(30)
([datetime]'2026-12-04').DayOfWeek
Get-Date '2026-12-04' -Format 'dddd, MMMM d'
Get-Date -Format 'yyyy-MM-dd'
```

```text
Monday, December 7, 2026 12:00:00 AM
Friday
Friday, December 4
2026-11-09
```

The last line is the day this lesson was written; yours is today. A date written `12/04/2026` is December 4 in the US and April 12 in much of the world, and `Get-Date '12/04/2026'` reads it the way your computer's region does. Year-month-day has no such doubt. Everything here works the same in Windows PowerShell 5.1 and on a Mac or Linux, though the way a date is shown follows your region.

**Try it.** How many days until your next birthday, or your next trip? Put its date in place of `2026-12-04` and count from today with `(Get-Date).Date`.

## Check yourself

2 questions to try on the lesson page.

## So what

Text that looks like a number or a date isn't one until you say so. Square brackets turn it into the right type, and from then on PowerShell adds money in exact cents and counts the days to anything.

## Key terms

- [Type Conversion](https://www.ask-codey.com/glossary/type-conversion-powershell)
- [Get-Date](https://www.ask-codey.com/glossary/get-date)
- [.NET Types in PowerShell](https://www.ask-codey.com/glossary/dotnet-types-in-powershell)
- [TimeSpan](https://www.ask-codey.com/glossary/timespan)

## Cheat sheets

- [Numbers, Dates and Types](https://www.ask-codey.com/cheat-sheets/powershell-types-cheat-sheet)

## Related

- [Text: Quotes and \`$( )\`](https://www.ask-codey.com/lessons/powershell-strings)
- [Properties and Methods](https://www.ask-codey.com/lessons/powershell-properties-and-methods)
- [Saving Data: Export-Csv and Cleaning a CSV](https://www.ask-codey.com/lessons/powershell-export-csv)

Source: https://www.ask-codey.com/lessons/powershell-types
