PowerShell

(Get-Date).AddDays along with Format switch

We commonly use the switch -Format along with Get-Date commandlet to adjust the date format. However, its not really possible to club date format change along with .adddays() method.

(Get-Date -Format yyyyMMdd).AddDays(x)

This is because of the fact that Format switch will change the type to “STRING” while changing the date format. And .adddays() is a method for the type DateTime.

While the commandlet is executing, Formatting happens first and then trying to use the method .adddays(). Which will fail as expected.

Method invocation failed because [System.String] does not contain a method named ‘AddDays’.

The error message is pretty straight. Method AddDays is not found within [Systems.String]. So the question is how to deal with this? The traditional option is to store the changed date to a variable first and then do the formatting as the second step.

So lets look at some more ways to accomplish in a single line. Possible?

  1. Call the Get-Date command along with adddays method inside Get-Date commandlet with formatting

2. Use method .adddays first and then .ToString

This is simple, but which one is better?

(Get-Date).AddDays(x).ToString(‘yyyyMMdd’) is slightly better as its approximately one millisecond quicker to finish.

Here are some intresting artcles to read on Get-Date and the options around it

https://adamtheautomator.com/powershell-get-date/

https://shellgeek.com/get-date-in-powershell/

Posted by Shabarinath in PowerShell, 0 comments

Install-Module : A parameter cannot be found that matches parameter name ‘AllowPrerelease’.

Microsoft is releasing new versions of PowerShell modules frequently now. And the real truth is many of the commandlets are having bugs these days than the earlier days (my personal opinion, not based on any statistics). In parallel, new services are getting launched and new modules are coming up to support them. Many times, We are forced to use the pre-release version to see if bug is getting fixed on the upcoming version.

The most common approach is to install the latest version directly from online repository if the client machines has access to internet. And the commandlet to be used is Install-Module with an additional switch -AllowPrerelease. However, Its common to end up with the below error, especially for the newer versions.

Install-Module : A parameter cannot be found that matches parameter name ‘AllowPrerelease’.
At line:1 char:37

  • Install-Module -Name MicrosoftTeams -AllowPrerelease
  • ~~~~
    • CategoryInfo : InvalidArgument: (:) [Install-Module], ParameterBindingException
    • FullyQualifiedErrorId : NamedParameterNotFound,Install-Module

The commandlet is not accepting the switch -AllowPrerelease.

As per the release notes, Minimum PowerShell version is 5.1 and I was running 5.1.17763.1971 on my server, still ended up with this error.

Fix?

Go ahead and Install Powershell 7.x.

You can install pre-release versions through Powershell 7. -PreRelease switch works fine with Powershell 7.

An additional error will come up to include -Force to install a pre-release version. With that, We are good to go !

Posted by Shabarinath in PowerShell, 0 comments