PowerShell – Working on a template build – rough draft

Just another Tech site

PowerShell – Working on a template build – rough draft

I am working on updating a process for installing software…I like the audio process for letting me know where in the code I am at in the process.

Rough draft see if I can figure a way to streamline this process.

####################################################################
##

Write-Host "`n"
Write-Host "                    -------------------------------------------------            " -ForegroundColor Cyan
Write-Host "                    |                                               |            " -ForegroundColor Cyan
Write-Host "                    |          Installer: MSI                       |            " -ForegroundColor Cyan
Write-Host "                    |          Application:                         |            " -ForegroundColor Cyan
Write-Host "                    |          Version: 1.0                         |            " -ForegroundColor Cyan
Write-Host "                    |                                               |            " -ForegroundColor Cyan
Write-Host "                    |          Author: Batman                       |            " -ForegroundColor Cyan
Write-Host "                    |          Date Created: 2/9/2025               |            " -ForegroundColor Cyan
Write-Host "                    |          Date Modified: 2/9/2025              |            " -ForegroundColor Cyan
Write-Host "                    |                                               |            " -ForegroundColor Cyan
Write-Host "                    -------------------------------------------------            " -ForegroundColor Cyan
Write-Host "`n"

##
####################################################################
## currently logged on user
$CurrUser = [System.Environment]::GetEnvironmentVariable('username')
$SystemName = [Environment]::MachineName

## Audio text to speech

Add-Type -AssemblyName System.Speech
$Speech = New-Object System.Speech.Synthesis.SpeechSynthesizer
$Speech.SelectVoice("Microsoft Hazel Desktop")
$Speech.Speak("Hello $($CurrUser) on $($SystemName)")

####################################################################
##
$Speech.Speak("Preparing for installation")

####################################################################
## Install Variables
$App = "7-Zip"
$App1 = ""
$App2 = ""
$InstallFile = "7z2409-x64.msi"

####################################################################
## Version 
$CurrVer = "2409"

## Folder Path
$folderPath = "C:\Windows\_MECMCache\$($App)\$($CurrVer)"
$LogPath = "C:\Logs"
$Installedpath32 = "C:\Program Files (x86)\"
$Installedpath64 = "C:\Program Files\"

####################################################################
####################################################################
## Define the path to the folder

## Check if the folder exists
if (-Not (Test-Path -Path $folderPath)) {
    # Create the folder if it does not exist
    New-Item -Path $folderPath -ItemType Directory
    Write-Output "`n"
    Write-Output "Folder created at $folderPath"
    Write-Output "`n"
} else {
    Write-Output "`n"
    Write-Output "Folder already exists at $folderPath"
    Write-Output "`n"
}


## Check if the Log folder exists
if (-Not (Test-Path -Path $LogPath)) {
    # Create the folder if it does not exist
    New-Item -Path $LogPath -ItemType Directory
    Write-Output "`n"
    Write-Output "Folder created at $LogPath"
    Write-Output "`n"
} else {
    Write-Output "`n"
    Write-Output "Folder already exists at $LogPath"
    Write-Output "`n"
}

$InstallfilePath = $folderPath + ".\$($Installfile)"
$InstalledAppPath32 = "C:\Program Files (x86)\"
$InstalledAppPath64 = "C:\Program Files\"


[System.Diagnostics.FileVersionInfo]::GetVersionInfo("$($InstalledAppPath)").FileVersion

####################################################################
## Get the date from today minus 30 days
$TimeDate = (Get-Date)

####################################################################
## Stopping Service

# services to stop
$services = @("$($App)", "$($App1)", "$($App2)")

foreach ($service in $services) {
    # Get the service object
    $svc = Get-Service -Name $service -ErrorAction SilentlyContinue
    
    if ($svc -and $svc.Status -eq 'Running') {
        # Stop the service if it is running
        Stop-Service -Name $service -Force
        Write-Output "Stopped service: $service"
    } else {
        Write-Output "Service $service is not running or does not exist."
    }
}

####################################################################
## Uninstall
$Speech.Speak("Uninstalling")



## Clean up installation version folders
$dateTime = (Get-Date).AddDays(-90)
$path = "C:\Windows\_MECMCache\$($App)"

Get-ChildItem -Path $Path -Recurse -Directory | Where-Object { $_.LastWriteTime -lt $dateTime } | Remove-Item -Recurse #-WhatIf



####################################################################
##
$Speech.Speak("Installing")

####################################################################
##
$Speech.Speak("Verifying Installation")

####################################################################
##
$Speech.Speak("Installation Completed")