NoBeep.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

<#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


}