Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pleabargain/e56353a7e3bdb4c7c09cb9ed11b14fd0 to your computer and use it in GitHub Desktop.
Save pleabargain/e56353a7e3bdb4c7c09cb9ed11b14fd0 to your computer and use it in GitHub Desktop.
powershell script to list and then prompt user to delete installed ollama models
# Ollama Model Manager Script
function Show-Models {
$models = ollama list
$index = 0
$models | ForEach-Object {
Write-Host "$($index): $_"
$index++
}
}
function Get-ModelName {
param (
[string]$modelLine
)
# Split the line by whitespace and take the first part which is the model name
if ($modelLine -match "^([^\s]+)") {
return $matches[1]
}
return $null
}
function Delete-Models {
param (
[string]$modelNumbers
)
$modelNumbersArray = $modelNumbers -split ','
$modelsList = @(ollama list) # Convert to array to enable indexing
foreach ($number in $modelNumbersArray) {
$number = $number.Trim()
if ([int]::TryParse($number, [ref]$null)) {
$index = [int]$number
if ($index -ge 0 -and $index -lt $modelsList.Count) {
$modelLine = $modelsList[$index]
$modelName = Get-ModelName -modelLine $modelLine
if ($modelName) {
Write-Host "Are you sure you want to delete model '$modelName'? This action is permanent. (y/n)"
$confirmation = Read-Host
if ($confirmation -eq 'y') {
ollama rm $modelName
Write-Host "Model '$modelName' deleted."
} else {
Write-Host "Deletion of model '$modelName' canceled."
}
} else {
Write-Host "Could not parse model name from line: $modelLine"
}
} else {
Write-Host "Model number '$number' does not exist."
}
} else {
Write-Host "Invalid number: '$number'"
}
}
}
# Main script
Write-Host "Welcome to the Ollama Model Manager."
Write-Host "This script will help you list and delete Ollama models."
Write-Host "Please note that deleting a model is permanent."
do {
Write-Host "Here are your current models:"
Show-Models
$modelNumbers = Read-Host "Enter the numbers of the models you want to delete, separated by commas (or type 'exit' to quit)"
if ($modelNumbers -ne 'exit') {
Delete-Models -modelNumbers $modelNumbers
}
} while ($modelNumbers -ne 'exit')
Write-Host "Goodbye!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment