A curated set of PowerShell utilities designed to live alongside your project. These scripts handle common cleanup and maintenance tasks without cluttering your solution or duplicating logic.
The execution.policy script is included for new or freshly rebuilt systems where PowerShell’s default execution policy prevents scripts from running. Running it once ensures your environment is ready for automation.
The get.app.vars script centralizes project‑specific names and paths so other scripts can import them rather than redefining the same values repeatedly. This keeps your automation consistent, maintainable, and easy to update.
Execution Policy Fix
Sets execution policy for all scopes.
clear-host
set-executionpolicy unrestricted -scope localmachine -force
set-executionpolicy unrestricted -scope currentuser -force
set-executionpolicy unrestricted -scope process -force
Set application variables globally
This script must reside in the same folder as any calling script; declare shared app variables once and reuse them everywhere.
Usage:$script = $PSScriptRoot\get.app.vars.ps1";.
.$script
write-host "`n=== Loading app vars" -foregroundcolor yellow -backgroundcolor black
$project = "BlazorApp1"
$schema = "db_blazorapp"
$basepath = "C:\Users\???\OneDrive\Projects\$project"
$publishpath = "C:\Users\???\Publish\$project"
$dbpath = "$basepath\Database"
$scripts = "$dbpath\scripts"
$views = "$dbpath\views"
$procs = "$dbpath\stored procedures"
$server = $env:computername
Attach A SQL Server database
It checks whether a SQL Server database exists, and if it doesn’t, it automatically attaches it from existing MDF/LDF files and sets the correct database owner.
function attachifnotdbexists {
$database = get-sqldatabase -serverinstance $server -name $schema -erroraction silentlycontinue
if ($database -eq $null) {
$query = @"
use [master];
create database [$schema] on
(
filename = N'$basepath\Database\Schema\$schema.mdf'
),
(
filename = N'$basepath\Database\Schema\$schema.ldf'
)
for attach;
if exists (
select name
from master.sys.databases sd
where sd.name = N'$schema'
and suser_sname(sd.owner_sid) = suser_sname()
)
exec [$schema].dbo.sp_changedbowner @loginame = N'$server\Mark', @map = false;
"@
invoke-sqlcmd -query $query
write-host "`n=== $schema attached " -foregroundcolor green -backgroundcolor black
}
else {
write-host "`n=== $schema already attached " -foregroundcolor white -backgroundcolor red
}
}
$script = $psscriptroot + "\get.app.vars.ps1"
. $script
clear-host
attachifnotdbexists
Detach A SQL Server database
It checks whether a SQL Server database exists, and if it does, it shrinks it, forces it into single‑user mode, and cleanly detaches it from the server.
function detachifdbexists {
$database = get-sqldatabase -serverinstance $server -name $schema -erroraction silentlycontinue
if ($database -eq $null) {
write-host "`n=== $schema not found " -foregroundcolor white -backgroundcolor red
}
else {
$query = @"
use [$schema];
dbcc shrinkdatabase (N'$schema');
dbcc shrinkfile (N'$schema', 8);
use [master];
alter database [$schema] set single_user with rollback immediate;
exec master.dbo.sp_detach_db @dbname = N'$schema', @skipchecks = 'false';
"@
invoke-sqlcmd -query $query
write-host "`n=== $schema detached " -foregroundcolor green -backgroundcolor black
}
}
$script = $psscriptroot + "\get.app.vars.ps1"
. $script
clear-host
detachifdbexists
Solution Cleanup Routine
It detaches the database, stops the running project processes, and performs a full cleanup of the solution by deleting bin/obj folders, removing misc user files, and rebuilding the .vs folder while preserving essential Visual Studio state.
clear-host
$script = join-path $psscriptroot "get.app.vars.ps1"
. $script
write-host "`n=== Detaching $schema" -foregroundcolor cyan -backgroundcolor black
$script0 = join-path $basepath "db.detach.ps1"
. $script0
stop-process -name $project -force -erroraction silentlycontinue
stop-process -name "vbcscompiler" -force -erroraction silentlycontinue
function safe-clear {
param([string]$path)
if (test-path $path) {
remove-item $path -recurse -force -erroraction silentlycontinue
}
else {
write-host "`n=== Path not found: $path" -foregroundcolor yellow -backgroundcolor black
}
}
function clean-vsfolder {
param(
[string]$solutionroot,
[string]$solutionname
)
$vsfolder = join-path $solutionroot ".vs"
$solutionfolder = join-path $vsfolder "$solutionname.slnx"
$preservefolder = join-path $solutionfolder "v18"
if (-not (test-path -literalpath $preservefolder)) {
write-host "`n=== Preserve folder not found: $preservefolder" -foregroundcolor red -backgroundcolor white
return
}
# Backup v18 folder to temp.
$backup = join-path $env:temp "vs-preserve-backup"
if (test-path $backup) { remove-item $backup -recurse -force }
copy-item $preservefolder $backup -recurse -force
write-host "`n=== Backed up v18 to $backup" -foregroundcolor green -backgroundcolor black
# Delete entire .vs folder.
remove-item $vsfolder -recurse -force -erroraction silentlycontinue
write-host "`n=== Deleted entire .vs folder" -foregroundcolor yellow -backgroundcolor black
# Recreate solution folder and restore v18.
new-item -itemtype directory -path $solutionfolder -force | out-null
copy-item $backup $preservefolder -recurse -force
write-host "`n=== Restored v18 to $solutionfolder\v18" -foregroundcolor green -backgroundcolor black
# Cleanup temp backup.
remove-item $backup -recurse -force -erroraction silentlycontinue
}
write-host "`n=== Clearing misc files" -foregroundcolor cyan -backgroundcolor black
safe-clear (join-path $basepath "*.user")
# Auto-detect and clear all project folders with bin/obj.
$projects = get-childitem -path $basepath -directory |
where-object {
(test-path (join-path $_.fullname "bin")) -or
(test-path (join-path $_.fullname "obj"))
}
foreach ($proj in $projects) {
write-host "`n=== Clearing $($proj.name)" -foregroundcolor cyan -backgroundcolor black
$bin = join-path $proj.fullname "bin"
$obj = join-path $proj.fullname "obj"
if (test-path $bin) { safe-clear $bin }
if (test-path $obj) { safe-clear $obj }
}
write-host "`n=== Cleaning .vs folder" -foregroundcolor cyan -backgroundcolor black
clean-vsfolder -solutionroot $basepath -solutionname $project
write-host "`n=== Clearing task complete" -foregroundcolor white -backgroundcolor black