NoBeep.ps1


<#PSScriptInfo
 
.VERSION 2.2
 
.GUID a4f43660-6b31-4db7-8b12-9c2ecc9e67cd
 
.AUTHOR
    David Zentner
    dazcode@gmail.com
 
.COMPANYNAME
    dazcode.com
 
.COPYRIGHT
    David Zentner
 
.TAGS
    nobeep
    beep
    audio
    console
    backspace
    delete
    sound
    mute
    speaker
    speakers
    powershell
    disable
    stop
    turnoff
    off
    setting
    profile
     
 
.LICENSEURI
 
.PROJECTURI
    http://dazcode.com
 
.ICONURI
https://www.powershellgallery.com/Content/Images/packageDefaultIcon.png
 
.EXTERNALMODULEDEPENDENCIES
 
.REQUIREDSCRIPTS
 
.EXTERNALSCRIPTDEPENDENCIES
 
.RELEASENOTES
    Disables the audio beep when a user presses backspace in the powershell console. Will prompt to save this setting to a user profile for future sessions.
#>



<#
 
.DESCRIPTION
 
Disables the audio beep when user presses backspace in the powershell console. Will prompt to save this setting to a user profile for future sessions.
 
Mutes Audio Beep Sound Backspace Delete Console PowerShell
 
 
written by:
David Zentner
dazcode@gmail.com
http://dazcode.net
 
 
#>
 


$var1 = (Get-PSReadlineOption).BellStyle


if($var1 -eq "None"){
    Write-Host "`r`nBeep is already disabled for this console session!" -ForegroundColor Yellow
    Write-Host "You can view the current beep setting with command: " -ForegroundColor Yellow -NoNewline;
    Write-Host "(Get-PSReadlineOption).BellStyle" -ForegroundColor Green

    exit
}


Write-Host "`r`nDisabling beep..." -ForegroundColor Yellow
Set-PSReadlineOption -BellStyle None
Write-Host "[done]" -ForegroundColor Green

$ProfileToEdit=$PROFILE.CurrentUserAllHosts
$SaveToProfile=$false
Write-Host "`r`nSave this setting to your user profile? " -ForegroundColor Yellow
Write-Host "Profile location: $ProfileToEdit" -ForegroundColor Gray


$Readhost = Read-Host "Yes(Y) / No(n)" 
    Switch ($ReadHost) 
     { 
       Y {$SaveToProfile=$true}
       YES {$SaveToProfile=$true}
       N {$SaveToProfile=$false}
       NO {$SaveToProfile=$false}       
       Default {$SaveToProfile=$true} 
     }
     
if ($SaveToProfile -eq $false) {
    exit
}
else {

    
    $Pathexists = Test-Path $ProfileToEdit
    
    
    write-host "`r`n" -NoNewline

    if ($Pathexists -eq $true) {
        #write-host "Found profile: $ProfileToEdit"
    }
    else {
        #write-host "User profile doesn't exist yet" -ForegroundColor Gray
        write-host "Creating new profile"
        new-item -type file -path $ProfileToEdit -force -ErrorAction Stop | Out-Null
        Write-Host "[done]" -ForegroundColor Green
    }

    write-host "Updating profile"
    $AppendOutput="Set-PSReadlineOption -BellStyle None #Added by NoBeep."
    Out-File -FilePath $ProfileToEdit -Append -InputObject $AppendOutput | Out-Null
    Write-Host "[done]" -ForegroundColor Green
    Write-Host "Opening file in notepad"
    Write-Host "[done]" -ForegroundColor Green
    

    notepad $ProfileToEdit


}