Quick Tip: Allow A Null Value For An Object That Doesn’t Normally Allow It

In the PowerShell Slack channel (powershell.slack.com) a question came up along the lines of “I have a script that needs to pass a datetime object, but sometimes I’d like that datetime object to be null”. Never mind that maybe the script could be re-architected. Let’s solve this problem.

The issue is, if you try to assign a null value to a datetime object, you get an error.

The solution is super easy. Just make the thing nullable.

This will return no output. So when you’re declaring the variable that will hold your datetime object, just make sure you make it nullable.

Just for more proof this works as advertised, try this.