I think you make things overly complex by providing default values ββfor your arguments and then providing error messages when the default values ββare used. If you want the parameters to be required, you must declare them as such. In fact, you can go further and declare them as a valid path and number in a certain range.
Otherwise, my main observation will be that New-Item creates any parent items that it needs, so you don't need to create monthly folders separately.
There are various ways to create a path string, but I think in this case it is easiest to format the month and day, and then just use a single string (note that as _
is a valid character in a variable name, in some cases you should use the t21 extension form ):
param( # Path to an existing folder. [Parameter(Mandatory=$True)] [ValidateScript({Test-Path $_ -PathType 'Container'})] [string]$inppath, # Year must be fairly recent or in the future. Warranty expires 2100 [Parameter(Mandatory=$True)] [ValidateRange(1999,2100)] [int]$inpyear ) 1..12 | ForEach-Object { $month = "{0:D2}" -f $_ 1..[DateTime]::DaysInMonth($inpyear,$month) | ForEach-Object { $day = "{0:D2}" -f $_ New-Item -ItemType directory -Force -Path "$inppath\${inpyear}_$month\${inpyear}_${month}_${day}" } }
Another thing is that if you want to use this often, it would be better to turn it into a function or cmdlet, and then you can save it in a module with other cmdlets. Also, if you do this, the comment before each parameter will become part of the help screen, and you can include a description and examples for the help screen in the comment at the top of the function declaration.
PS For bloody beginners, I recommend http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start
source share