Running CMD command in PowerShell

Try this: & “C:\Program Files (x86)\Microsoft Configuration Manager\AdminConsole\bin\i386\CmRcViewer.exe” PCNAME To PowerShell a string “…” is just a string and PowerShell evaluates it by echoing it to the screen. To get PowerShell to execute the command whose name is in a string, you use the call operator &.

The response content cannot be parsed because the Internet Explorer engine is not available, or

In your invoke web request just use the parameter -UseBasicParsing e.g. in your script (line 2) you should use: $rss = Invoke-WebRequest -Uri $url -UseBasicParsing According to the documentation, this parameter is necessary on systems where IE isn’t installed or configured: Uses the response object for HTML content without Document Object Model (DOM) parsing. This … Read more

Echo equivalent in PowerShell for script testing

There are several ways: Write-Host: Write directly to the console, not included in function/cmdlet output. Allows foreground and background colour to be set. Write-Debug: Write directly to the console, if $DebugPreference set to Continue or Stop. Write-Verbose: Write directly to the console, if $VerbosePreference set to Continue or Stop. The latter is intended for extra … Read more

Powershell Formatting for a String

The reason your current attempt doesn’t work is that single-quoted (‘) string literals in PowerShell are verbatim strings – no attempt will be made at expanding subexpression pipelines or variable expressions. If you want an expandable string literal without having to escape all the double-quotes (“) contained in the string itself, use a here-string: $mynumber … Read more

Output file doesn’t match Write-Host

Caveat: Write-Host is meant for to-display output, not for outputting data – it bypasses PowerShell’s success output stream (PowerShell’s stdout equivalent), so that output from Write-Host cannot (directly[1]) be captured in a variable, nor redirected to file – see the bottom half of this answer for more information. Use Write-Output or – preferably – PowerShell’s … Read more

Powershell ConvertFrom-Json Encoding Special Characters Issue

Peter Schneider’s helpful answer and Nas’ helpful answer both address one problem with your approach: You need to: either: access the .Content property on the response object returned by Invoke-WebRequest to get the actual data returned (as a JSON string), which you can then pass to ConvertFrom-Json. or: use Invoke-RestMethod instead, which returns the data … Read more

Get the drive letter of USB drive in PowerShell

Try: gwmi win32_diskdrive | ?{$_.interfacetype -eq “USB”} | %{gwmi -Query “ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`”$($_.DeviceID.replace(‘\’,’\\’))`”} WHERE AssocClass = Win32_DiskDriveToDiskPartition”} | %{gwmi -Query “ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`”$($_.DeviceID)`”} WHERE AssocClass = Win32_LogicalDiskToPartition”} | %{$_.deviceid} Tested with one and more than one USB device plugged-in.

Why does “Get-ChildItem -File | Get-FileHash” work?

[*] The System.IO.FileInfo / System.IO.DirectoryInfo instances output by PowerShell cmdlets have a .PSPath property[*] that contains the instances’ fully qualified path, which is the full file-system path prefixed by the PS provider name (e.g., Microsoft.PowerShell.Core\FileSystem::C:\windows). File-processing cmdlets such as Get-FileHash have a -LiteralPath parameter which has an alias name of -PSPath. Because a -LiteralPath parameter … Read more