Windows – WMIC Command works, but doesn’t work in powershell script

batch-filenetworkingpowershellwindows

#Get Data Rate
wmic "/OUTPUT:'C:\Users\user\Documents\DATE RATE\Data_Rate-ts8"+".txt" nic where 'NetEnabled = True' 'get Name,MacAddress,Speed'"

Running this in the console directly works. That is only because in the console, I can do this:

ps C:\>wmic
wmic:root\cli>/OUTPUT:"C:\Users\user\Documents\DATE RATE\Data_Rate-ts8"+".txt" nic netenabled ="True"
wmic:root\cli>exit
ps C:\>

In the powershell script, I can't simply type WMIC first and then the rest of the command or else it interrupts the script by entering the WMIC prompt, waiting for user to type and is no longer executing the script.

When I put it in a powershell script (first snippet), I keep getting:

No Alias Found

I've tried different combinations of quotes. But the script I posted above specifically gives me:

"/OUTPUT:'C:\Users\user\Documents\DATE RATE
TESTS\Data_Rate-ts8'+'.txt' nic where 'NetEnabled = True' 'get Name –
Alias not found.

Best Answer

Are you determined to continue using WMIC? If you're using powershell you may find it more useful to use the native powershell cmdLets to obtain the same information.

Depending on what you're doing with the file, this may allow you to skip the file. You could also pipe the WMI query results to | foreach { }

get-wmiobject Win32_NetworkAdapter -filter 'NetEnabled = True' | select-object Name,MacAddress,Speed | format-list | out-file dataRate.txt