Tag Archives: powershell

No hdmi sound without OS reboot? Restart the drivers…

Edit: end july 2015 the problem is now fixed for me with windows 10.

This post blog follows a problem that I encountered when I upgraded my laptop to windows 8.1. Precisely, the hdmi sound stops working immediately when I connect the hdmi cable to my television. The hardware works fine because if the OS is reboot with hdmi cable plugged, then the TV as a sound playback device is available. This is terribly annoying, you do not want to reboot each time you want to use your TV as a display device. While searching the internet for a solution, I discovered that I am not the only one with this problem. Therefore, I will try to expose my solution with many details in order to make it reusable by others who would stumble on this post with the same problem.

Assuming that you are in the same, or a close, situation: the hdmi sound works well when Windows is reboot with the cable plugged in. Consequently, when everything is working, if you right click on the sound icon in the tray bar and select “playback devices” you should be able to see the TV in the device playbacks .

The list of available playback devices.

The list of available playback devices.

Before going any further the first thing to do is to update the drivers. This is quite simple, right click on the windows menu at the bottom left of your desktop screen and select the device manager. My advice is to update all drivers for the two entries in the tree view “display adapters” and “sound, video and game controllers”.

The device manager

The device manager

Once all drivers have been updated, check that the problem still occurs (restart the OS once at least) and if it does you may be interested in the following.

Upgrading driver software...

Upgrading driver software

So even after updating the drivers, you still need the reboot to get the sound. Actually, the drivers managing the hdmi need to be restarted and that is what we are going to do manually, without restarting the OS. To perform such action, we are going to need devcon.exe which is a tiny program distributed freely by Microsoft with the Windows Driver Kit. Download it here (you do not need Visual studio).

After the (silent) installation of WDK you may find devcon.exe in the the path “C:\Program Files (x86)\Windows Kits\8.1\Tools\x64”. You will notice that you have two directories in Tools the “/x64” and the “/x86” (i.e 32 bits), use the one corresponding to your system (to know more on 32 bits vs 64 bits check this).

WARNING: devcon.exe error messages are extremely misleading (read completely wrong…). Indeed, if you are using the x86 version instead of the x64 version or if you are trying to restart the drivers (see below) without administrators right you will still have the same error.: “No matching devices found”. Consequently, do not try to interpret the error raised by devcon.exe.

listmedia

“devcon.exe listclass media” executed in powershell using conemu terminal

Now, we are going to restart the two drivers using devcon.exe. First, let us see all drivers dedicated to sound, then type in your command prompt (or better with powershell) run as administrator: <pathToDevCon>/devcon.exe listclass media

The response here is a list of two items, we are going to retain the first one, “Intel (R) display”, which was the audio adapter used for the hdmi (see the first screen shot of this post). The driver Id: HDAUDIOFUNC_01&VEN_8086&DEV_2807&SUBSYS_80860101&REV_10004&36161A1D&0&0001 is going to be needed. If you are using powershell you may use the Out-File command to write it down in a text file…

You can try to restart it directly by typing (in command line run as administrator): <pathToDevCon>/devcon.exe restart “@<DRIVER_ID>”.
Note that the @ symbol is inside the quotes which may surprise you if you are a .NET developer…

Unfortunately, it does not seem to be sufficient and the system also needs to reboot the display adapter driver after that. So we are going to do the same action but for display adapter driver: we type in an admin command line:
<pathToDevCon>/devcon.exe listclass display

devcon.exe listclass display

devcon.exe listclass display

Then now if you restart the driver listed here as “Intel(R) HD Graphics Family” then the HDMI sound device playback should be available if the cable is connected. To do so execute:

<pathToDevCon>/devcon.exe restart “@PCIVEN_8086&DEV_0A16&SUBSYS_130D1043&REV_093&11583659&0&10”

The Id above is mine, obviously you should use yours.

Fine but this is not over yet. Indeed, you do not want to do all this each time you are connecting your hdmi cable with your TV. What we are going to do right now, is to add a powershell script that will automated this process so that you will just have to click on one icon when you will want your TV sound. If you have never run powershell script on your machine, then the execution of ps1 file (powershell script) will not be allowed. Read this to enable powershell script execution (basically you will just have to type in a powershell console, run as admin, Set-ExecutionPolicy Unrestricted).

Here is the script that you can use. I have added two instructions to enable the drivers, it seems to be sometimes useful when you plug/unplug the hdmi cable several times. All you have to do to make it work is to change the path to devcon.exe and the id. You can save it to the place of your choice such as the Desktop with a .ps1 extension (e.g. restart_drivers.ps1)

Remind that to be effective this .ps1 file should be executed with administrator rights. Remind also, that devcon.exe will only tell you that the drivers cannot be found if you are not with admin rights. So I am suggesting to make the script more easily run in admin mode, you can put a powershell.exe shortcut next to the .ps1 script file. By right clicking on the shortcut file go to Properties and fill the target property as follows. Target:”powershell.exe restart_drivers.ps1″. Then, in Advanced… select Run as administator.

A shortcut to run restart_drivers.ps1 directly with admin right

A shortcut to run restart_drivers.ps1 directly with admin right

That is all, no when you will connect your hdmi cable you will only have to click on the shortcut and enjoy the sound from your TV.

 

Powershell + ConEmu + PowerTab: a good combo

Edit: this post was written before the release of PSReadLine which seems to be a more reasonable choice than PowerTab for powershell.exe enhancement (completion, intellisense etc.). Indeed, this latter project looks like to be dead now and does not seem to be as rich as the former one. Regarding the rest of the post below, I encourage you to use Conemu console which is still far better than the usual CommandPrompt (this may change with the release of Win10). In addition, you may skip the “Aliasing part” because command in PATH are well recognized by PSReadLine which was not the case for PowerTab. You will discover also a lot of interesting features proposed by PsReadLine such as syntax coloring, custom key bindings….

Microsoft has released in 2006 Powershell (PoSh), a brilliant shell based on the .NET framework. However, the console (or terminal) stays really close to the usual windows command prompt. It is commonly admitted that this console suffers a lot of drawbacks which makes it painful for a everyday usage, see for example these blogs (artlogic , hanselman, etc.). Just to make things clear, let us recall the difference between a shell and a console. The former is the command interpreter, it is performing the hard work while the latter is just the window, i.e. the program in charge of prompting and displaying information.

conemu

ConEmu with PowerTab tabbing

In this post I will show you how to configure ConEmu an opensource console emulator to work with Powershell and to benefit from a well designed tab completion with PowerTab.

We will only refer to the latest version of Powershell, the 3.0 released in 2012. Let us mention that Powershell 3.0 came with a new version of Powershel ISE, the PoSh scripting environment. In this third version, ISE has Intellisense which is a great feature, especially when you are discovering PoSh. Unfortunately, ISE console does not support tabbing and suffers the same drawbacks has the original windows command prompt. In addition, ISE is perfectly suited when you are writing some script but is not really adapted to perform every day tasks. In one word ISE more an IDE than a console. In order to have a substitute to Intellisense in ConEmu we will use the PowerTab project.

You may start right now by checking that you have Powershell 3.0 installed (how to check version). Right after, you may start downloading ConEmu.

If you are an intensive Powershell user, you would want ConEmu to start directly with a new PoSh session. Nothing is more simple, just add the following .txt file somewhere in your disk (e.g. next to ConEmu .exe file).

shortcutProp

Target in properties of the desktop/taskbar shortcut

Next, create a shortcut in task bar or desktop, right click on it on go to the properties of  the link. You may enter the following command in the target property: <pathToConemu>conemu.exe /cmd @<pathtoyourfile>/startfile.txt

So now you would have a nice console which starts automatically with Powershell. Let us go a little bit further by importing PowerTab in ConEmu. Visit PowerTab website and download the sources rather than the packaged version. Indeed, many fixes are present in the source code but not packaged in the .zip. At the time of the writing I use version of commit 035310b4c93e, Follow the instruction for installing Powertab. Quickly it is: unzipping source under <YourHomePath>/WindowsPowerShell/Modules/PowerTab and execute command Import-Module PowerTab.

While importing PowerTab module, it will ask you if you want to import PowerTab on start. This config will be saved by updating your $PROFILE. If your not familiar with PoSh profiles now, I suggest you to read this. To sum up the $PROFILE is a special PoSh script executed each time your entering a new Powershell session (then each time you are starting ConEmu if you have done what is above). This is where you would put some custom scripts and any in-house configuration. NB: under commit 035310b4c93e, the function of your $PROFILE are recognized by PowerTab which does not seemed to be the case on the packaged version PowerTab 0.99.6.

Now we do have a console with efficient tabbing. The style is much more old school than the ISE intellisense but it does the job well and is quite pleasant to use.

I am not done yet, actually, our version of PowerTab does not see the .exe in you PATH environment variable for tabbing. This is quite frustrating not to have auto-complete for vim, notepad++, tracecrt.exe etc. This may be included in incoming version (I will see what I can do…) but here is workaround. As long as PowerTab recognizes Powershell command aliases, we are going to build an alias dictionary that includes the aforementioned .exe located in the PATH environment variable.

The following snippet shows a part of your $PROFILE where the aliases are imported just before the module PowerTab. They are imported from a file alias.txt which is located next to the $PROFILE .ps1 file. You create this alias.txt file by invoking the function Export-AliasWithEXEInPATH. You have to call this function at least once and to keep your aliases up-to-date you should run it when you modify the PATH variable. Remark the lightweight PoSh syntax for the .NET hashtable: @{}

Even if it will probably be outdated soon, this is my configuration at home and at work for now and I am quite pleased of it.

F#-ception, wtf ?!?

220px-Inception_ver3Hi, its middle of summer so let us relax with a “coding for fun” post. The subject presented here came from a kind of “wtf contest” that we had with a colleague of mine on our free time. The objective was to write a unit test to tackle an existing code base using the weirdest approach. My contestant Gabriel is a brilliant developer, so in order to compete with him I had to bring something really absurd rather than technically sophisticated. So I came with the idea of executing F# code inlined in a powershell script invoked from F# code. Obviously, this is absolutely useless. However, it fitted well for the thematic of the  contest. In this post we will skip the “existing code base” part and focus on how to achieve this with a very simple example .

The first part of the job is to write some F# code that builds the powershell script with inlined F#. We will have to reference the F# code dom provider which can be downloaded here. Remark that for the sake of simplicity of this post we did not add any extra assemblies (see referencedAssemblies in script below).

Then let us try the FsToPs function with very simple case

Thus, the powershell code in simpleExample.ps1 looks like

You should be able to run this script in a powershell session. However, it is not finished for us, we have to invoke this script in F# and retrieve the returned value. The following snippet will do the job for us.

Then executed within MsTest framework….

…it works!

TestPassed

I’d like to thank Gabriel for being fair play by helping me to debug the first version of F#-ception.