Building Directories with PowerShell
I needed to create a script that will build out directories following a pattern. In this case, Microsoft Security Updates. Perfect opportunity to practice a little coding. The script needs to be functioning in an all Windows environment, and easy to maintain. Sadly, this eliminated Python, which is more interesting to me, but PowerShell is a very close second. And despite my desire to not be a Windows guy, the almighty paycheck comes from a company that builds a product that runs exclusively on Windows. So away we go(sanitized where appropriate).
# get current year from system and format for directories
$year = get-date -format yyyy
$year_short = get-date -f yy
$month = get-date -format MMMM
$month_short = get-date -format MM
# constants; current month name and prefix
$parent_dir = "$month_short - $month"
$prefix = "\MS$year_short-"
# actual, live working directory
$checkifdir = "\\servername.domain\software\patches\$year\$parent_dir"
# testing/debugging directory
#$checkifdir = "C:\Users\wkopp\Desktop\temp\sandbox\$year\$parent_dir"
# check if current month directory exists, if not, create (w/loop)
# this happens without user input
if ($checkifdir -eq $false){
md $checkifdir
}
# ask user for range of this month's bulletins(user input)
# create the names for directories needed.
# create directories
$st = Read-Host "Please enter the number of the first bulletin: "
$end = Read-Host "Please enter the number of the last bulletin: "
$bulletin_range = $st..$end
for ($i = 0; $i -lt $bulletin_range.length; $i++){
$string_name = $prefix + $bulletin_range[$i].ToString("000")
$folder = $checkifdir + $string_name
Write-Host $folder
if ($folder -ne $false){
md $folder
}
}