How to play mp3 with powershell (simple)? - powershell

How do I play mp3 with Powershell?
What I tried:
powershell -c (New-Object Media.SoundPlayer "Track.mp3").Play();

You can play an mp3 from powershell, you just need to use
system.windows.media.mediaplayer
instead of
System.Media.SoundPlayer
It works in Windows 10
I followed a snipped from http://eddiejackson.net/wp/?p=9268
# Play a single file
Add-Type -AssemblyName presentationCore
$mediaPlayer = New-Object system.windows.media.mediaplayer
$mediaPlayer.open('C:\sounds\bike.mp3')
$mediaPlayer.Play()

This is an old question but I just went through this so if anyone is interested in learning or knowing how to do this in a more simple manner use the following answer.
As long as you have a media player that has good command line switches such as VLC and other similar types of versatile players (perhaps media player classic) you CAN make a one line command to run a MP3 file. Here is my example, enjoy! :)
& 'C:\Program Files\VideoLAN\VLC\vlc.exe' --qt-start-minimized --play-and-exit --qt-notification=0 "D:\SystemSettings\51Hz.mp3"
This would run 51Hz.mp3 file with neither notifications to take place nor any other user interaction.

This works only for wav files:
$Song = New-Object System.Media.SoundPlayer
$Song.SoundLocation = "path\to\track.wav"
$Song.Play()

Simplest way is to just use the Invoke-Item cmdlet, it will open whatever Windows has set as the default handler for the file type. When I used it on an mp3 file it opened Groove Music. I'm sure if I installed winamp or mpc or whatever and set the default to that it would open in there instead.

From URL
$MediaPlayer = [Windows.Media.Playback.MediaPlayer, Windows.Media, ContentType = WindowsRuntime]::New()
$MediaPlayer.Source = [Windows.Media.Core.MediaSource]::CreateFromUri('https://nyanpass.com/nyanpass.mp3')
$MediaPlayer.Play()
From File
$MediaPlayer = [Windows.Media.Playback.MediaPlayer, Windows.Media, ContentType = WindowsRuntime]::New()
$MediaPlayer.Source = [Windows.Media.Core.MediaSource]::CreateFromUri('C:\Users\Admin\Downloads\nyanpass.mp3')
$MediaPlayer.Play()

SoundPlayer can only play *.wav files. There is no easy 1 line way of playing mp3 files. However this can be done by writing a script using the MediaPlayer class.

This worked for me
$Song = New-Object System.Media.SoundPlayer
$Song.SoundLocation = "path\to\track.mp3"
$Song.Play()
I guess you cannot pass the file location through new-object

I can confirm this works:
$player = New-Object System.Media.SoundPlayer "$env:windir\Media\notify.wav"
$player.Play()
Source: http://community.idera.com/powershell/powertips/b/tips/posts/playing-wav-files

Related

Change icon for file from PowerShell

I am trying to create the URL file with a custom icon but, for some reason, it is not working.
This is my code:
#Downloading ico file
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile("url location of the icon file","C:\Users\Public\Pictures\filename.ico")
#Creating URL file
$wshShell = New-Object -ComObject "WScript.Shell"
$urlShortcut = $wshShell.CreateShortcut(
(Join-Path $wshShell.SpecialFolders.Item("AllUsersDesktop") "myname.url")
)
$urlShortcut.TargetPath = "https://somewebsite"
$urlShortcut.IconLocation = "C:\Users\Public\Pictures\filename.ico"
$urlShortcut.Save()
The icon file is downloaded, and the URL file is created but, the image is not changed. I've tried a few different things with no luck.
Would be nice if anyone has some input on this.
Kind regards,
The .IconLocation property isn't supported in your case, but there's a workaround:
# ... icon download code omitted
$shortcutFile = Join-Path $wshShell.SpecialFolders.Item('AllUsersDesktop') 'myname.url'
$iconFile = 'C:\Users\Public\Pictures\filename.ico'
$wshShell = New-Object -ComObject "WScript.Shell"
$urlShortcut = $wshShell.CreateShortcut($shortcutFile)
$urlShortcut.TargetPath = 'https://en.wikipedia.org'
$urlShortcut.Save()
# This updates the .url file directly to emulate what assigning
# an icon interactively, via File Explorer, does.
#"
IconIndex=0
HotKey=0
IconFile=$iconFile
"# | Add-Content -LiteralPath $shortcutFile
When you create a URL shortcut file (extension .url):
Only one writable property is supported by the resulting WshUrlShortcut object, namely TargetPath, which stores the target URL.
Notably, this prevent use of the IconLocation property, which is only available on executable shortcut files (extension .lnk), which are WshShortcut objects.
However, the .url file format does support custom icons (by default, the default browser's icon is used), but that requires assigning them interactively, via File Explorer.
Fortunately, .url files are plain-text, .ini-like files, so it's easy to programmatically update that file directly, so as to emulate the results of interactively assigning an icon, as shown above.
Alternatively - which may or may not be an option in your case - you can create a regular shortcut file, with extension .lnk, which - perhaps surprisingly - also works with URLs assigned to .TargetPath. Assigning to .IconLocation then works as usual.
However, there are ramifications:
Obviously, you'll end up with a different filename extension, and the shortcut file won't be readily recognizable as a URL shortcut by its extension.
.lnk files are binary files.
.lnk files with URLs as their target path inexplicably don't allow the URL to be edited via File Explorer later.

Download multiple large files asynchronously with Powershell

I have four large OS installation media I need to download. This will take a long time if I wait for each download to finish before moving to the next one.
Before downloading, I want to check if the media is already present.
The solution is likely a combination of a hash table, test-path and invoke-webrequest but I just can't crack it.
So in pseudo something on the lines of:
Check if file1 exists
if true then download and check file2
if false check file 2
check if file 2 exists...
So check if files exists and if not, start downloading all the ones that are missing.
I'm not very experienced with PS so all help is much appreciated, thank you very much! Researching the answer was fun but I feel I'm missing a keyword here...
There is a fairly simple way for async downloads using WebClient class, although it's probably not available on older version of PS. See the example below
$files = #(
#{url = "https://github.com/Microsoft/TypeScript/archive/master.zip"; path = "C:\temp\TS.master.zip"}
#{url = "https://github.com/Microsoft/calculator/archive/master.zip"; path = "C:\temp\calc.master.zip"}
#{url="https://github.com/Microsoft/vscode/archive/master.zip"; path = "C:\temp\Vscode.master.zip"}
)
$workers = foreach ($f in $files) {
$wc = New-Object System.Net.WebClient
Write-Output $wc.DownloadFileTaskAsync($f.url, $f.path)
}
# wait until all files are downloaded
# $workers.Result
# or just check the status and then do something else
$workers | select IsCompleted, Status

PowerShell Script to play video with Windows Media Player

I have a requirement to run a video via windows Media Player. And also track the duration it play.
Suppose I close the video in 5 Sec it should give the duration 5. The below is the script I wrote. But there is problem with this. As the video do not launch nor I am able to the application getting launched . I am only able to here the audio.
Add-Type -AssemblyName presentationCore
$filepath = [uri] "C:\Temp\test\Wildlife.wmv"
$wmplayer = New-Object System.Windows.Media.MediaPlayer
$wmplayer.Open($filepath)
Start-Sleep 2 # This allows the $wmplayer time to load the audio file
$duration = $wmplayer.NaturalDuration.TimeSpan.Seconds
$wmplayer.Play()
Start-Sleep $duration
$wmplayer.Stop()
$wmplayer.Close()
Write-Host $duration
Please help...
Regards,
Suman Rout
You need to be creating a form that shows up, then creating a VideoDrawing, then a DrawingBrush, and then applying it as the background of some portion of the form. From my understanding, MediaElement is easier to use - but regardless you're not starting media player here, you're using Windows Media objects without creating a form to display them on.
If you merely mean to open the video and close it, try launching the Windows Media Player application instead. I used your code and did something like maybe you're intending:
Add-Type -AssemblyName presentationCore
$filepath = "C:\Temp\test\Wildlife.wmv"
#Here we use your code to get the duration of the video
$wmplayer = New-Object System.Windows.Media.MediaPlayer
$wmplayer.Open($filepath)
Start-Sleep 2
$duration = $wmplayer.NaturalDuration.TimeSpan.Seconds
$wmplayer.Close()
#Here we just open media player and play the file, with an extra second for it to start playing
$proc = Start-process -FilePath wmplayer.exe -ArgumentList $filepath -PassThru
Start-Sleep ($duration + 1)
#Here we kill the media player
Stop-Process $proc.Id -force
Write-Host $duration

Powershell: Passing command options forward slash changing to backward

I am trying to use the invoke (ii) command to open an access database that has command line options. What I would like to have executed is below (yes there is a space in the name of the access database). The database is in the same folder as the Powershell script.
What I want: program name.accdb /cmd Rester
What I get: program name.accdb \cmd Rester
The exact commands I am using are:
$Path_To_EXE = "program name.accdb /cmd Rester"
&ii $Path_To_EXE
I am new to Powershell and have done some searching but can't seem to find an answer. I can create a work around by creating a separate .bat file but that seems like going backwards.
Thoughts?
You should also give a shot to the start-process cmdlet :
$Path_To_EXE = "c:\program.exe"
#Notice the simple quotes ...
$Arguments = #( "name.accdb", '/cmd' , "Rester" )
start-process -FilePath $Path_To_EXE -ArgumentList $Arguments -Wait
I'm not quite sure of the format of the answer you'll get tough ...
for database interaction, I'll rather use JGreenwell's Approach, since the answer that you'll get will be much easier to read/debug ...
Let me know if it works.
If you want to run a VBA script while passing it a parameter with powershell:
$aApp = New-Object -ComObject access.application
$aApp.Application.OpenCurrentDatabase("some program.accdb")
$aApp.Application.Run("VBAScriptName", [ref] "Raster")
First according to Microsoft Support you can use ;; for /cmd from the command line. Second because of the way call quotes and dequotes variables you have to include the /cmd flag separate from the variable (well, its the easiest way). Third, you might consider creating a new com-object to handle running Access with Powershell as it allows for a lot more options (just ask and I can add some examples of this). This being said try:
$Path_To_EXE = "program name.accdb"
&ii $Path_To_EXE ;;Rester #Try ;;"Rester" if it doesn't work.
#if that works then its a problem in Rester
#fyi another way is:
$Path_To_EXE = #("program name.accdb", ";;Rester")
&ii $Path_To_EXE
If you want to use an ActiveX Object Controller to open and perform operations on Access look at this blog from technet <- Read the link there are pitfalls to avoid.
$adOpenStatic = 3
$adLockOptimistic = 3
$objConnection = New-Object -com "ADODB.Connection"
$objRecordSet = New-Object -com "ADODB.Recordset"
$objConnection.Open("Provider = Microsoft.Jet.OLEDB.4.0; Data Source = C:\Scripts\Test.mdb")
$objRecordset.Open("Select * From Computers", $objConnection,$adOpenStatic,$adLockOptimistic)
$objRecordSet.AddNew()
$objRecordSet.Fields.Item("ComputerName").Value = "atl-ws-001"
$objRecordSet.Fields.Item("SerialNumber").Value = "192ATG43R"
$objRecordSet.Update()
$objRecordSet.Close()
$objConnection.Close()

Generating printer shortcuts from list

I am trying to generate a shortcut for every printer I have on a print server. The idea is to be able to email these shortcuts to people and when they click on them, it automatically installs that printer for them.
I've populated an array from a list of printer names exported from the print server:
$list = #((get-contnet $home\dekstop\plist.txt))
I then created a method to create a shortcut:
function Make-Shortcut
{
param ([string]$dest, [string]$source)
$WshShell = New-Object -comObject Wscript.Shell
$Shortcut = $WshShell.CreateShortcut($dest)
$Shortcut.TargetPath = $Source
$Shortcut.Save()
}
The function works fine. I was able to create standard shortcuts with no problem.
This next part is where I am getting stuck:
foreach ($i in $list)
{
Make-Shortcut "C:\pshort\$i.lnk" "C:\Windows\System32\rundll32.exe
printui.dll,PrintUIEntry /in /q /n\\printserver\$i"
}
When this runs, it does generate a shortcut with the same name as the printer for each printer on the list. However, the problem comes in at the target path. Instead of
C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry /in /q /n\\printserver\printername
it changes it to:
C:\Windows\System32\rundll32.exe printui.dll,PrintUIEntry \in \q \n\printserver\printername
The three problems with this are:
It is reversing the forward slash for the parameters
It is removing one of the backslashes preceding the server name
It is adding quotes to both sides. I need the quotes to come off for the shortcut to work properly.
I assume this is happening because Powershell thinks I am trying to make a standard shortcut and thinks I made mistakes while typing out the path.
I have tried putting a ` in front of each forward slash hoping the escape character would prevent it from reversing it, but no luck. I also tried using a hyphen for each parameter but that did not work either.
Is there anyway to stop this from happening? Or is there perhaps a better way to try to accomplish what I am trying to do?
You need to add arguments to the com object
Try adding a new param $arguments to your Make-Shortcut function and do:
Make-Shortcut "C:\pshort\$i.lnk" "C:\Windows\System32\rundll32.exe"
"printui.dll,PrintUIEntry /in /q /n\\printserver\$i"
add this in your function:
$Shortcut.Arguments = $arguments
So the link is created successfully ... but I have no idea if it works :)
Completely different answer but in a standard windows environment simply clicking a hyperlink to \printserver\printer will add a shared printer to someone's system?
So an email that simply lists :
\\PrintServer\Printer01
\\PrintServer\Printer02
\\PrintServer\Printer03
Would probably do the job just as well.