Set sound volume level on login

Kasual

Well-Known Member
Messages
1,485
Reaction score
508
Sometimes people set high (or max) volume level and forget to set volume to low on shut down.

After a few years of no activity at all, I'm "rusted" on windows customization.

Has anyone asked as a feature an option to set volume to a custom level on boot?
 
Volume Boot Control

Run PowerShell / Terminal as Administrator, paste the script:
Bash:
$p="$env:LocalAppData\Programs\NirCmd\nircmd.exe";$d=Split-Path $p;$x="$d\volsvc.exe";$n="SetVolSvc";$r="HKLM:\SYSTEM\CurrentControlSet\Services\$n\Parameters";$k='HKCU:\Software\Microsoft\Windows\CurrentVersion\Run';$m='HKCU:\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\MuiCache'
while($true){$i=(Read-Host 'Volume 0-100 | d=delete | q=quit').Trim().ToLower()
if($i -eq 'q'){Write-Host 'Exit' -ForegroundColor Yellow;[Console]::Beep(500,80);[Console]::Beep(300,120);exit}
if($i -eq 'd'){if(Test-Path $k){(Get-Item $k).Property|?{$_ -like 'SetVolume*'}|%{rp $k $_ -ea 0}};if(Test-Path $m){(Get-Item $m).Property|?{$_ -like '*nircmd*' -or $_ -like '*volsvc*'}|%{rp $m $_ -ea 0}};sc.exe stop $n *>$null;sc.exe delete $n *>$null;rm $d -r -fo -ea 0;rm "$env:TEMP\nircmd.zip" -fo -ea 0;rm "$env:WINDIR\Prefetch\NIRCMD.EXE-*.pf" -fo -ea 0;rm "$env:WINDIR\Prefetch\VOLSVC.EXE-*.pf" -fo -ea 0;Write-Host 'Deleted' -ForegroundColor Yellow;[Console]::Beep(600,80);[Console]::Beep(400,120);continue}
$v=0;if([int]::TryParse($i,[ref]$v) -and $v -ge 0 -and $v -le 100){
if(-not(Test-Path $p)){$pp=$ProgressPreference;$ProgressPreference='SilentlyContinue';[Net.ServicePointManager]::SecurityProtocol=[Net.SecurityProtocolType]::Tls12;mkdir $d -Force|Out-Null;try{iwr https://www.nirsoft.net/utils/nircmd-x64.zip -OutFile "$env:TEMP\nircmd.zip" -ea Stop;Expand-Archive "$env:TEMP\nircmd.zip" $d -Force -ea Stop}catch{Write-Host 'Download failed' -ForegroundColor Red};$ProgressPreference=$pp}
if(-not(Test-Path $x)){$c=@("$env:WINDIR\Microsoft.NET\Framework64\v4.0.30319\csc.exe","$env:WINDIR\Microsoft.NET\Framework\v4.0.30319\csc.exe")|?{Test-Path $_}|Select -First 1;if(-not $c){$c=(gci "$env:WINDIR\Microsoft.NET" -r -fi csc.exe -ea 0|Select -First 1).FullName}
if($c){@'
using System;using System.ServiceProcess;using System.Diagnostics;using System.Threading;using Microsoft.Win32;
class V:ServiceBase{protected override void OnStart(string[] a){try{using(var s=new ServiceController("AudioSrv"))s.WaitForStatus(ServiceControllerStatus.Running,TimeSpan.FromSeconds(15));Thread.Sleep(200);using(var k=Registry.LocalMachine.OpenSubKey("SYSTEM\\CurrentControlSet\\Services\\SetVolSvc\\Parameters")){if(k!=null){string e=k.GetValue("NirCmdPath")as string;int v=Convert.ToInt32(k.GetValue("Volume")??0);if(e!=null&&System.IO.File.Exists(e)){var p=new ProcessStartInfo(e,"setsysvolume "+v){UseShellExecute=false,CreateNoWindow=true};var r=Process.Start(p);if(r!=null)r.WaitForExit(5000);}}}}catch{}Stop();}protected override void OnStop(){}static void Main(){Run(new V());}}
'@|Out-File "$d\v.cs" -Encoding UTF8;& $c /nologo /reference:System.ServiceProcess.dll /out:$x "$d\v.cs" 2>$null;rm "$d\v.cs" -fo -ea 0}else{Write-Host 'csc.exe not found' -ForegroundColor Red}}
if((Test-Path $p) -and (Test-Path $x)){$s=[int][math]::Floor($v*655.35+.5);sc.exe query $n *>$null;if($LASTEXITCODE -ne 0){sc.exe create $n binPath= "`"$x`"" start= auto obj= LocalSystem type= own *>$null;sc.exe query $n *>$null;if($LASTEXITCODE -ne 0){Write-Host 'Service create failed' -ForegroundColor Red;continue}};if(-not(Test-Path $r)){mkdir $r -Force|Out-Null};sp $r NirCmdPath $p;sp $r Volume $s -Type DWord;& $p setsysvolume $s;Write-Host "Volume $v%" -ForegroundColor Green;[Console]::Beep(900,100)}else{Write-Host 'nircmd/volsvc not found' -ForegroundColor Red}}else{Write-Host 'Invalid input' -ForegroundColor Red}}
Sets Windows volume to a specified level before the welcome sound plays at boot. Works through a Windows service SetVolSvc that starts during the service loading phase — before winlogon and the shell. On first numeric input, the script downloads the NirCmd utility (nirsoft.net - 152KB), compiles the service volsvc.exe using the built-in Windows csc.exe compiler, and registers it via sc.exe. Subsequent inputs only update the volume in the service registry.

Input: 0–100 | Set volume: immediately + at every boot

Input: d | Full removal: stop and delete the SetVolSvc service, NirCmd folder, archive, HKCU:\...\Run entries, MUICache, Prefetch files

Input: q | Exit the script
 
Too much info o_O
internet needed
How can I add it into windows unattended install image without internet set up?
 
Bash:
@echo off
setlocal
set "VOL=30"
set "CS=%TEMP%\_v.cs"
set "EX=%WINDIR%\System32\volsvc.exe"
set "SVC=SetVolSvc"
set "KEY=HKLM\SYSTEM\CurrentControlSet\Services\%SVC%"

net session >nul 2>&1 || goto :end
sc stop %SVC% >nul 2>&1
ping -n 3 127.0.0.1 >nul 2>nul

echo using System;using System.ServiceProcess;using System.Runtime.InteropServices;using System.Threading;using Microsoft.Win32;> "%CS%"
echo [ComImport,Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")]class E{}>> "%CS%"
echo [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]interface D{int _a(int a,int b,out IntPtr c);int GetDefaultAudioEndpoint(int f,int r,out IntPtr d);}>> "%CS%"
echo [Guid("D666063F-1587-4E43-81F1-B948E807363F"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]interface V{int Activate(ref Guid i,int c,IntPtr p,[MarshalAs(UnmanagedType.IUnknown)]out object o);}>> "%CS%"
echo [Guid("5CDF2C82-841E-4546-9722-0CF74078229A"),InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]interface A{int _0();int _1();int _2();int _3();int SetMasterVolumeLevelScalar(float l,Guid g);int _5();int GetMasterVolumeLevelScalar(out float l);int _7();int _8();int _9();int _10();int SetMute([MarshalAs(UnmanagedType.Bool)]bool m,Guid g);int GetMute(out bool m);}>> "%CS%"
echo class S:ServiceBase{void W(){try{using(var sc=new ServiceController("AudioSrv"))sc.WaitForStatus(ServiceControllerStatus.Running,TimeSpan.FromSeconds(30));int v=30;using(var k=Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Services\SetVolSvc\Parameters")){if(k!=null)v=Convert.ToInt32(k.GetValue("Volume")??30);}v=Math.Max(0,Math.Min(100,v));int t=0;while(t!=10){try{var e=(D)(new E());IntPtr d;e.GetDefaultAudioEndpoint(0,1,out d);var dv=(V)Marshal.GetObjectForIUnknown(d);var i=typeof(A).GUID;object o;dv.Activate(ref i,1,IntPtr.Zero,out o);var av=(A)o;av.SetMute(false,Guid.Empty);av.SetMasterVolumeLevelScalar(v/100f,Guid.Empty);break;}catch{Thread.Sleep(500);}t++;}}catch{}Stop();}protected override void OnStart(string[] a){new Thread(W){IsBackground=true}.Start();}protected override void OnStop(){}static void Main(){Run(new S());}}>> "%CS%"

set "CSC=%WINDIR%\Microsoft.NET\Framework64\v4.0.30319\csc.exe"
if not exist "%CSC%" set "CSC=%WINDIR%\Microsoft.NET\Framework\v4.0.30319\csc.exe"
if not exist "%CSC%" goto :end
"%CSC%" /nologo /reference:System.ServiceProcess.dll /out:"%EX%" "%CS%" >nul 2>&1
if not exist "%EX%" goto :end

sc create %SVC% binPath= "%EX%" start= auto obj= LocalSystem type= own >nul 2>&1
sc config %SVC% binPath= "%EX%" start= auto obj= LocalSystem type= own >nul 2>&1
sc description %SVC% "Sets master volume to a fixed level before logon" >nul 2>&1
reg add "%KEY%\Parameters" /v Volume /t REG_DWORD /d %VOL% /f >nul 2>&1

del "%CS%" >nul 2>&1
sc query AudioSrv 2>nul | find "RUNNING" >nul && sc start %SVC% >nul 2>&1

:end
endlocal

Save the file as SetVolSvc.cmd with ANSI encoding.

No internet, no external tools, no NirCmd

Change volume: line 3 → set "VOL=30" (0–100)
 
Kasual
Stops and removes the SetVolSvc service, deletes volsvc.exe and all related registry keys, restoring default Windows volume behavior (remembers last level set before shutdown):
Bash:
cmd /c "sc stop SetVolSvc >nul 2>&1 & sc delete SetVolSvc >nul 2>&1 & del /f C:\Windows\System32\volsvc.exe >nul 2>&1 & reg delete HKLM\SYSTEM\CurrentControlSet\Services\SetVolSvc /f >nul 2>&1 & del /f C:\Windows\Prefetch\VOLSVC.EXE-*.pf >nul 2>&1 & echo removed"
 
Back
Top