How to auto-hide the taskbar from the command line - windows

Does anyone know how I can automatically hide the task bar in windows 7 via command line or some other method?

To autohide the taskbar from a cmd prompt or in a .cmd or. bat file:
Windows 7 (StuckRects2)
powershell -command "&{$p='HKCU:SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StuckRects2';$v=(Get-ItemProperty -Path $p).Settings;$v[8]=3;&Set-ItemProperty -Path $p -Name Settings -Value $v;&Stop-Process -f -ProcessName explorer}"
Windows 10 (StuckRects3)
powershell -command "&{$p='HKCU:SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\StuckRects3';$v=(Get-ItemProperty -Path $p).Settings;$v[8]=3;&Set-ItemProperty -Path $p -Name Settings -Value $v;&Stop-Process -f -ProcessName explorer}"
Explanation
The registry key which stores this value also stores a number of other settings. Since we only want to change position 9 ($v[8] in the cmd) of that registry setting, we need to preserve the other settings.
Normally from cmd, it's enough to use a reg add command to modify the registry, but we use powershell because it makes it easy to preserve the other settings stored under the same registry key.
Explorer also needs to be restarted to pick up the change. We use Stop-Process because Windows automatically restarts Explorer when it is stopped.
Note: change $v[8]=3 to $v[8]=2 in the commands above to undo this change (if you want the taskbar to be always visible).

Here's a little C program that will toggle the hidden/shown state of the taskbar window. Note that when it's hidden it's actually gone from the screen completely (it's not in auto-hide mode).
#include <windows.h>
int main() {
HWND hwnd = FindWindow("Shell_traywnd", "");
if (IsWindowVisible(hwnd))
SetWindowPos(hwnd,0,0,0,0,0,SWP_HIDEWINDOW);
else
SetWindowPos(hwnd,0,0,0,0,0,SWP_SHOWWINDOW);
return 0;
}
Using SHAppBarMessage. This one toggles the autohide state.
#include <windows.h>
#include <shellapi.h>
// This isn't defined for me for some reason.
#ifndef ABM_SETSTATE
#define ABM_SETSTATE 0x0000000A
#endif
int main() {
APPBARDATA abd = {sizeof abd};
UINT uState = (UINT) SHAppBarMessage(ABM_GETSTATE, &abd);
LPARAM param = uState & ABS_ALWAYSONTOP;
if (uState & ABS_AUTOHIDE)
abd.lParam = param;
else
abd.lParam = ABS_AUTOHIDE | param;
SHAppBarMessage(ABM_SETSTATE, &abd);
return 0;
}

Related

Open file explorer at coordinates

When I boot up my computer I open several file explorers and sort them around the screen to help speed up my workflow. It's not time consuming, only tedious, and I'd like a small program to do it for me. I know how to open an explorer, but I don't see any positional arguments.
Is there a way to spawn a file explorer at a set of screen coordinates, or move it programatically after it opens? Preferably with python 3+, but batch will work as well.
That was simultaneously easier and harder than I thought it was going to be. Everything is commented, let me know if you have any more questions. This is a PowerShell/batch hybrid script (so save it as a .bat file) because PowerShell is disabled on systems by default or something.
<# :
:: Based on https://gist.github.com/coldnebo/1148334
:: Converted to a batch/powershell hybrid via http://www.dostips.com/forum/viewtopic.php?p=37780#p37780
:: Array comparison from http://stackoverflow.com/a/6368667/4158862
#echo off
setlocal
set "POWERSHELL_BAT_ARGS=%*"
if defined POWERSHELL_BAT_ARGS set "POWERSHELL_BAT_ARGS=%POWERSHELL_BAT_ARGS:"=\"%"
endlocal & powershell -NoLogo -NoProfile -Command "$_ = $input; Invoke-Expression $( '$input = $_; $_ = \"\"; $args = #( &{ $args } %POWERSHELL_BAT_ARGS% );' + [String]::Join( [char]10, $( Get-Content \"%~f0\" ) ) )"
goto :EOF
#>
# Create an instance of the Win32 API object to handle and manipulate windows
Add-Type #"
using System;
using System.Runtime.InteropServices;
public class Win32 {
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
}
"#
# Get a list of existing Explorer Windows
$previous_array = #()
$shell_object = New-Object -COM 'Shell.Application'
foreach($old_window in $shell_object.Windows())
{
$previous_array += $old_window.HWND
}
# Open four more Explorer Windows in the current directory
explorer
explorer
explorer
explorer
# Pause for 1 second so that the windows have time to finish opening
sleep 1
# Get the list of new Explorer Windows
$new_array = #()
foreach($new_window in $shell_object.Windows())
{
$new_array += $new_window.HWND
}
# Compare the two arrays and only process the new windows
$only_new = Compare-Object -ReferenceObject $previous_array -DifferenceObject $new_array -PassThru
# MoveWindow takes HWND value, X-position on screen, Y-position on screen, window width, and window height
# I've just hard-coded the values, adjust them to suit your needs
[Win32]::MoveWindow($only_new[0],0,0,960,540,$true)
[Win32]::MoveWindow($only_new[1],960,0,960,540,$true)
[Win32]::MoveWindow($only_new[2],0,540,960,540,$true)
[Win32]::MoveWindow($only_new[3],960,540,960,540,$true)

How to close window with .bat script?

I have ran into a slight problem while configuring some keybinds with my mouse.
The thing I want to achieve is to open the program from the backround(open or closed window) and close it again, but not kill the task(eqivalent to the x-button, so it keeps running in the backround)
I have set up my mouse to start up a application called everything (which is able of very fast file system searches)
I have figured out that with this code:
TASKKILL /F /IM Everything.exe
which I can run with a macro key I can kill the application. But I dont want to kill the application itself just the window, becase it takes a while to index all the important files once I have to restart it.
So heres the question is there any way to bring up a window/task to the front of the screen or trigger the x button event, without having to kill the entire process ?
Also the Mouse(g600) supports lua scripting, but that is proabbly the more harder way to go about it.
Minimizing a window can be usually done by activating the window (bringing it to the foreground) then using SendKeys() to send Alt+Space, then whatever letter your locale has underlined for "Minimize" (N in English Windows). Here's one of possibly dozens of examples of this using VBScript.
That sequence is overused, inelegant, and boring in my opinion -- and it doesn't work for some windows (the cmd console, for example). It is possible to minimize a window without bringing it to the foreground, and without simulating key presses. One simply needs to import a function from user32.dll. And as long as we're going through that trouble, we might as well import a second function to detect the current window state so we can toggle minimized / restored.
We can import the functions using PowerShell like this. Save this Batch / PowerShell hybrid script with a .bat extension and give it a run.
<# : minimize.bat
:: toggles minimized state of a window by its filename
:: minimize.bat /? for usage
:: https://stackoverflow.com/a/34834953/1683264
#echo off & setlocal
if "%~1"=="" goto usage
set "prog=%~n1"
tasklist | findstr /i "\<%prog%\>" >NUL || goto usage
set /P "=Toggling the minimized state of %prog%... " <NUL
powershell -noprofile -noninteractive "iex ((gc \"%~f0\") -join \"`n\")"
goto :EOF
:usage
echo syntax: %~nx0 progname[.exe]
echo;
echo If the program is visible, minimize it. If minimized, restore it.
goto :EOF
:: end Batch / begin PowerShell hybrid chimera #>
Add-Type user32_dll #'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
'# -namespace System
$hwnd = #(Get-Process $env:prog)[0].MainWindowHandle
$state = [user32_dll]::GetWindowLong($hwnd, -16)
# mask of 0x20000000 = minimized; 2 = minimize; 4 = restore
if ($state -band 0x20000000) { $action = 4 } else { $action = 2 }
if ([user32_dll]::ShowWindowAsync($hwnd, $action)) {
write-host "Success" -f green
} else {
write-host "Fail" -f red
}
Now, if your program minimizes to the systray, the previous script will be fine for minimizing, but won't be able to find the window handle when minimized. It will fail when trying to restore. Finding the HWND in this situation is tricky. If one knows the window title, one can find the HWND using the user32.dll FindWindow() or FindWindowEx() functions.
I haven't had much luck using WMI or built-in PowerShell commands for finding the title of a window minimized to the tray. Neither get-process nor gwmi win32_process nor [diagnostics.process]::getProcessByName() yielded success. I did, however, discover that the final line of tasklist /v /fo list will show the window title. That's enough to invoke FindWindow() and get the HWND.
This might be more complicated than it needs to be, but then again it might not. I just know I found 1,000 ways to fail, but one way to succeed. Thanks to JPBlanc for helping me figure out how to pass null arguments into FindWindow() and FindWindowEx().
<# : minimize.bat
:: toggles minimized state of a window by its filename
:: minimize.bat /? for usage
#echo off & setlocal
if "%~1"=="" goto usage
set "prog=%~n1"
tasklist | findstr /i "\<%prog%\>" >NUL || goto usage
set /P "=Toggling the minimized state of %prog%... " <NUL
powershell -noprofile -noninteractive "iex ((gc \"%~f0\") -join \"`n\")"
goto :EOF
:usage
echo syntax: %~nx0 progname[.exe]
echo;
echo If the program is visible, minimize it. If minimized, restore it.
goto :EOF
End batch / begin PowerShell hybrid chimera #>
Add-Type user32_dll #'
[DllImport("user32.dll")]
public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(IntPtr lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter,
IntPtr lclassName, string windowTitle);
'# -namespace System
$hwnd = (ps $env:prog)[0].MainWindowHandle
if ($hwnd -eq 0) {
tasklist /v /fi "imagename eq $env:prog*" /fo list | %{
$title = $_ -replace '^[^:]+:\s+'
}
$zero = [IntPtr]::Zero
$hwnd = [user32_dll]::FindWindow($zero, $title)
if ($hwnd -eq 0) {
$hwnd = [user32_dll]::FindWindowEx($zero, $zero, $zero, $title)
}
}
$state = [user32_dll]::GetWindowLong($hwnd, -16)
# mask of 0x20000000 = minimized; 2 = minimize; 4 = restore
if ($state -band 0x20000000) { $action = 4 } else { $action = 2 }
if ([user32_dll]::ShowWindowAsync($hwnd, $action)) {
write-host "Success" -f green
} else {
write-host "Fail" -f red
}
can't comment so I'll try to answer.
the 'x' button will likely kill your application unless the application is configured otherwise I guess.
I think I understand you'd rather minimize it into icon tray or something. This is not possible as far as I know if your application isn't doing it naturally.
You could try to hit alt + space + n which is a shortcut to minimize the application.

How to close the personalization window after specified time via command prompt

I have some vbs code that will automatically change my windows theme via cmd as well as close it after the operation completes. The personalization window opens, Windows changes the theme, and then the personalization window closes. The problem is, sometimes the window doesn't close after changing the theme and I'm wondering why. Also, is there a one-liner code in cmd (or vbs that can execute through cmd) that just closes the personalization window? Thanks in advance for your help! My code used is as follows:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "rundll32.exe %SystemRoot%\system32\shell32.dll,Control_RunDLL %SystemRoot%\system32\desk.cpl desk,#Themes /Action:OpenTheme /file:""C:\Windows\Resources\Ease of Access Themes\basic.theme"""
Wscript.Sleep 1600
WshShell.AppActivate("Desktop Properties")
WshShell.Sendkeys "%FC"
WshShell.Sendkeys "{F4}"
Your Run call is being done asynchonously so your script will continue without waiting for Run to complete. This is fine, and it's what you need in your situation. But if it takes longer than 1600ms to launch the Desktop Properties dialog then AppActivate and your SendKeys commands are being sent to a nonexistent window. Have you tried increasing the sleep time to see if it works?
You can also test the availability of the window in a loop. AppActivate returns True if the window is found and False otherwise. For example, here's a snippet that tries for 10 seconds to see if the window appears (checking each second)...
For i = 1 To 10
WScript.Sleep 1000
If WshShell.AppActivate("Desktop Properties") Then
WshShell.Sendkeys "%FC"
WshShell.Sendkeys "{F4}"
Exit For
End If
Next
' If i > 10, it failed to find the window.
After trying a similar solution, I came up with the following powershell:
Function Get-WindowHandle($title,$class="") {
$code = #'
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
'#
Add-Type -MemberDefinition $code -Namespace MyWinAPI -Name GetWindowHandle
return [MyWinAPI.GetWindowHandle]::FindWindow($class, $title)
}
Function Close-WindowHandle($windowHandle) {
$code = #'
[System.Runtime.InteropServices.DllImport("User32.dll")]
public static extern bool PostMessage(IntPtr hWnd, int flags, int idk, int idk2);
'#
Add-Type -MemberDefinition $code -Namespace MyWinAPI -Name CloseWindowHandle
#https://msdn.microsoft.com/en-us/library/windows/desktop/ms632617(v=vs.85).aspx
$WM_CLOSE = 0x0010
return [MyWinAPI.CloseWindowHandle]::PostMessage($windowHandle, $WM_CLOSE, 0, 0)
}
Close-WindowHandle $(Get-WindowHandle 'Personalization' 'CabinetWClass')

Win32 console problem

is it possible to create a program that works as console application if started from the console and works as windows program (with GUI) when started otherwise?
If it is possible - how can I do that?
regards
Tobias
If you set the program up to build as a GUI program you can then attempt to attach to the console using AttachConsole(). You you attach OK then you were started from a console and you can proceed to redirect your standard handles to the newly attached console.
In this way you can start up and see if you are being started from a console that you can attach to and if so become a console program. If you cant attach you can show a GUI.
I've had some success with this, the main problem I have is redisplaying the command window's prompt when my program exits (which is how normal console programs operate), but I expect you could do something clever (read the console buffer on start up and find the prompt to redisplay when you exit?) if you really wanted to ...
If you need the program to act as a console application (e.g. print the usage information to the console) you must complile as a console application. A windows application will not have access to the console and cmd.exe will not wait for it to finish before printing the prompt and accepting the next command.
The best solution is to have two versions, one for command line and one for the GUI (which users usually run via a link on the desktop or start menu).
If you insist on using a single binary you will have to live with a console window appearing, at least for a short time. You can get rid of the console window using
FreeConsole();
You can tell that your application was run from GUI if it is the only process attached to the console. You can use GetConsoleProcessList to find the list of processes attached to the console.
This is the answer from Dan Tillett and it is remarkably effective. No flashes, no .com and .exe to trick cmd.exe. Seems to work flawlessly typing the command, in a .bat file, with focus, without focus and as double-click GUI app.
It's the bees knees!
Here is web page describing it, but I've posted it here because if that page goes 404 next month or 2 years from now, the excellent and "most complete" solution I've seen would be "off the grid".
http://www.tillett.info/2013/05/13/how-to-create-a-windows-program-that-works-as-both-as-a-gui-and-console-application/
#define WINVER 0x0501 // Allow use of features specific to Windows XP or later.
#define _WIN32_WINNT 0x0501
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <io.h>
#include <fcntl.h>
#include <stdio.h>
#pragma comment(lib, "User32.lib")
// Attach output of application to parent console
static BOOL attachOutputToConsole(void) {
HANDLE consoleHandleOut, consoleHandleError;
int fdOut, fdError;
FILE *fpOut, *fpError;
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
//redirect unbuffered STDOUT to the console
consoleHandleOut = GetStdHandle(STD_OUTPUT_HANDLE);
fdOut = _open_osfhandle((intptr_t)consoleHandleOut, _O_TEXT);
fpOut = _fdopen(fdOut, "w" );
*stdout = *fpOut;
setvbuf(stdout, NULL, _IONBF, 0 );
//redirect unbuffered STDERR to the console
consoleHandleError = GetStdHandle(STD_ERROR_HANDLE);
fdError = _open_osfhandle((intptr_t)consoleHandleError, _O_TEXT);
fpError = _fdopen(fdError, "w" );
*stderr = *fpError;
setvbuf(stderr, NULL, _IONBF, 0 );
return TRUE;
}
//Not a console application
return FALSE;
}
//Send the "enter" to the console to release the command prompt on the parent console
static void sendEnterKey(void) {
INPUT ip;
// Set up a generic keyboard event.
ip.type = INPUT_KEYBOARD;
ip.ki.wScan = 0; // hardware scan code for key
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
//Send the "Enter" key
ip.ki.wVk = 0x0D; // virtual-key code for the "Enter" key
ip.ki.dwFlags = 0; // 0 for key press
SendInput(1, &ip, sizeof(INPUT));
// Release the "Enter" key
ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
SendInput(1, &ip, sizeof(INPUT));
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR lpCmdLine, INT nCmdShow) {
int argc = __argc;
char **argv = __argv;
UNREFERENCED_PARAMETER(hInstance);
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
UNREFERENCED_PARAMETER(nCmdShow);
BOOL console;
int i;
//Is the program running as console or GUI application
console = attachOutputToConsole();
if (console) {
//Print to stdout
printf("Program running as console application\n");
for (i = 0; i < argc; i++) {
printf("argv[%d] %s\n", i, argv[i]);
}
//Print to stderr
fprintf(stderr, "Output to stderr\n");
}
else {
MessageBox(NULL, "Program running as windows gui application",
"Windows GUI Application", MB_OK | MB_SETFOREGROUND);
}
//Send "enter" to release application from the console
//This is a hack, but if not used the console doesn't know the application has returned
//"enter" only sent if the console window is in focus
if (console && GetConsoleWindow() == GetForegroundWindow()){
sendEnterKey();
}
return 0;
}
The program itself will never know how it was started. Unless you are willing to pass an execution arguments to the program. For example: program.exe -GUI ... you can capture the passed parameters and decide how the program should run based on parameters passed.
your program whould be something like:
class MainClass
{
public static int Main(string[] args)
{
// Test if input arguments were supplied:
if(args[0]=="GUI")
new myGUI().show(); //runs an instance of your gui
else
//you know what should go here
}
}
You can sort of guess whether you are started from the console or not by doing this:
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
fConsole = csbi.dwCursorPosition.X | csbi.dwCursorPosition.Y;
It's a guess -- if your cursor position is not 0,0 than you are in a console and can work as a console app. Otherwise go and create your windows.
Another way to guess is to look at the process tree and see what process launched your app. If it is cmd.exe go in console mode, otherwise go into GUI mode.
Make it a console application and put this into the code:
void ConsoleWindowVisible(bool show)
{
DWORD dummy;
if
(
!show && // Trying to hide
GetConsoleProcessList(&dummy, 1) == 1 // Have our own console window
)
ShowWindow(GetConsoleWindow, SW_HIDE); // Hide the window
else // Trying to show or use parent console window
ShowWindow(GetConsoleWindow, SW_NORMAL); // Show the window
}
int main(int argc, char** argv)
{
ConsoleWindowVisible(false);
}
Cheers.
gor.f.gyolchanyan#gmail.com

How can I mute/unmute my sound from PowerShell

Trying to write a PowerShell cmdlet that will mute the sound at start, unless already muted, and un-mute it at the end (only if it wasn't muted to begin with).
Couldn't find any PoweShell or WMI object I could use. I was toying with using Win32 functions like auxGetVolume or auxSetVolume, but couldn't quite get it to work (how to read the values from an IntPtr?).
I'm using V2 CTP2. Any ideas folks?
Thanks!
Starting with Vista you have to use the Core Audio API to control the system volume. It's a COM API that doesn't support automation and thus requires a lot of boilerplate to use from .NET and PowerShell.
Anyways the code bellow let you access the [Audio]::Volume and [Audio]::Mute properties from PowerShell. This also work on a remote computer which could be useful. Just copy-paste the code in your PowerShell window.
Add-Type -TypeDefinition #'
using System.Runtime.InteropServices;
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
// f(), g(), ... are unused COM method slots. Define these if you care
int f(); int g(); int h(); int i();
int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
int j();
int GetMasterVolumeLevelScalar(out float pfLevel);
int k(); int l(); int m(); int n();
int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
int GetMute(out bool pbMute);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
int f(); // Unused
int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
public class Audio {
static IAudioEndpointVolume Vol() {
var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
IMMDevice dev = null;
Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
IAudioEndpointVolume epv = null;
var epvid = typeof(IAudioEndpointVolume).GUID;
Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
return epv;
}
public static float Volume {
get {float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v;}
set {Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty));}
}
public static bool Mute {
get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
}
}
'#
Usage sample:
PS C:\> [Audio]::Volume # Check current volume (now about 10%)
0,09999999
PS C:\> [Audio]::Mute # See if speaker is muted
False
PS C:\> [Audio]::Mute = $true # Mute speaker
PS C:\> [Audio]::Volume = 0.75 # Set volume to 75%
PS C:\> [Audio]::Volume # Check that the changes are applied
0,75
PS C:\> [Audio]::Mute
True
PS C:\>
There are more comprehensive .NET wrappers out there for the Core Audio API if you need one but I'm not aware of a set of PowerShell friendly cmdlets.
P.S. Diogo answer seems clever but it doesn't work for me.
Use the following commands on a ps1 powershell script:
$obj = new-object -com wscript.shell
$obj.SendKeys([char]173)
Alexandre's answer fit my situation, but his example does not work due to compilation errors regarding the namespace of 'var'. It seems that newer/different versions of .net may cause the example not to work. If you found that you received compilation errors, this is an alternative version to try for those cases:
Add-Type -Language CsharpVersion3 -TypeDefinition #'
using System.Runtime.InteropServices;
[Guid("5CDF2C82-841E-4546-9722-0CF74078229A"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IAudioEndpointVolume {
// f(), g(), ... are unused COM method slots. Define these if you care
int f(); int g(); int h(); int i();
int SetMasterVolumeLevelScalar(float fLevel, System.Guid pguidEventContext);
int j();
int GetMasterVolumeLevelScalar(out float pfLevel);
int k(); int l(); int m(); int n();
int SetMute([MarshalAs(UnmanagedType.Bool)] bool bMute, System.Guid pguidEventContext);
int GetMute(out bool pbMute);
}
[Guid("D666063F-1587-4E43-81F1-B948E807363F"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDevice {
int Activate(ref System.Guid id, int clsCtx, int activationParams, out IAudioEndpointVolume aev);
}
[Guid("A95664D2-9614-4F35-A746-DE8DB63617E6"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IMMDeviceEnumerator {
int f(); // Unused
int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint);
}
[ComImport, Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] class MMDeviceEnumeratorComObject { }
public class Audio {
static IAudioEndpointVolume Vol() {
var enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
IMMDevice dev = null;
Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(/*eRender*/ 0, /*eMultimedia*/ 1, out dev));
IAudioEndpointVolume epv = null;
var epvid = typeof(IAudioEndpointVolume).GUID;
Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
return epv;
}
public static float Volume {
get {float v = -1; Marshal.ThrowExceptionForHR(Vol().GetMasterVolumeLevelScalar(out v)); return v;}
set {Marshal.ThrowExceptionForHR(Vol().SetMasterVolumeLevelScalar(value, System.Guid.Empty));}
}
public static bool Mute {
get { bool mute; Marshal.ThrowExceptionForHR(Vol().GetMute(out mute)); return mute; }
set { Marshal.ThrowExceptionForHR(Vol().SetMute(value, System.Guid.Empty)); }
}
}
'#
Usage is the same:
PS C:\> [Audio]::Volume # Check current volume (now about 10%)
0,09999999
PS C:\> [Audio]::Mute # See if speaker is muted
False
PS C:\> [Audio]::Mute = $true # Mute speaker
PS C:\> [Audio]::Volume = 0.75 # Set volume to 75%
PS C:\> [Audio]::Volume # Check that the changes are applied
0,75
PS C:\> [Audio]::Mute
True
PS C:\>
You could skin the cat another way by simply managing the Windows Audio Service. Stop it to mute, start it to unmute.
There does not seem to be a quick and easy way to adjust the volume.. If you have c++ experience, you could do something with this blog post, where Larry Osterman describes how to call the IAudioEndpointVolume interface from the platform api(for Vista, XP might be more difficult from what I've found in a few searches).
V2 does allow you to compile inline code (via Add-Type), so that might be an option.
I know it isn't PowerShell, but combining the answers from Michael and Diogo gives a one-line VBScript solution:
CreateObject("WScript.Shell").SendKeys(chr(173))
Slap this in mute.vbs, and you can just double-click to toggle mute
still works in Windows 10 (10586.104)
no need to Set-ExecutionPolicy as you might with PowerShell
Solution in vbscript:
Set WshShell = CreateObject("WScript.Shell")
For i = 0 To 50
WshShell.SendKeys(chr(174))
WScript.Sleep 100
Next
The keys reduce the volume by 2 each time.
I didn't find how to do this in PowerShell, but there is a command-line utility called NirCmd that will do the trick by running this command:
C:\utils\nircmd.exe mutesysvolume 0 # 1 to to unmute, 2 to toggle
NirCmd is available for free here:
http://www.nirsoft.net/utils/nircmd.html
Check out my answer to Change audio level from powershell?
Set-DefaultAudioDeviceMute
lemme try again, after getting some feedback on my answer
This is based on #frgnca's AudioDeviceCmdlets, found here: https://github.com/frgnca/AudioDeviceCmdlets
Here is code that will mute every recording device.
Import-Module .\AudioDeviceCmdlets
$audio_device_list = Get-AudioDevice -list
$recording_devices = $audio_device_list | ? {$_.Type -eq "Recording"}
$recording_devices
$recording_device_index = $recording_devices.Index | Out-String -stream
foreach ($i in $recording_device_index) {
$inti = [int]$i
Set-AudioDevice $inti | out-null -erroraction SilentlyContinue
Set-AudioDevice -RecordingMute 1 -erroraction SilentlyContinue
}
You import the AudioDeviceCmdlets dll, then save a list of all audio devices, filtered down into recording devices. You grab the index of all the recording devices and then iterate through each one, first setting the device to be your primary audio device, then setting that device to mute (this 2 step process is a limitation imposed by the dll).
To unmute everything, you change -RecordingMute 1 to RecordingMute 0
Similarly, to mute playback devices you can use this code:
Import-Module .\AudioDeviceCmdlets
$audio_device_list = Get-AudioDevice -list
$playback_devices = $audio_device_list | ? {$_.Type -eq "Playback"}
$playback_devices
$playback_device_index = $playback_devices.Index | Out-String -stream
foreach ($i in $playback_device_index) {
$inti = [int]$i
Set-AudioDevice $inti | out-null -erroraction SilentlyContinue
Set-AudioDevice -PlaybackMute 1 -erroraction SilentlyContinue
}
To unmute everything, you change -PlaybackMute 1 to PlaybackMute 0
This code comes from part of a bigger project I have which hooks up to a physical button/LED mute status display via Arduino to enable single button press to Mute/Unmute all system microphones (to help with Zoom, Meet, Teams, Discord, etc.) and to help people avoid embarrassing hot-mic incidents.
https://github.com/dakota-mewt/mewt
P.S. I really like some of the one-line solutions like Change audio level from powershell?
However, note that a single-line muting typically applies only to the single device that's currently set as the windows default. So if you have software that's capable of addressing different audio devices (like Zoom and Meet), and they happen to be using a non-default device, it may not work as desired.
In the second script above, to work in PowerShell 7 or PowerShell Core in the 1st line change:
-Language CsharpVersion3
to...
-Language Csharp
Working on W10
Here's a simple timed mute script using autohotkey (from autohotkey dotcom).
mbutton:: ; Trigger this script with a click on the mouse wheel
SoundSet, +1,, Mute ; Mute the PC sound
sleep, 27000 ; Wait 27 seconds for the annoying advertisement to run
SoundSet, +1,, Mute ; Unmute the PC sound

Resources