13

Shown below, an array works fine as input for Write-Output but not for Write-Debug (I expected them to be more similar than that).

PS C:\> [string[]]$test = @("test1", "test2", "test3")
PS C:\> Write-Output $test
test1
test2
test3
PS C:\> $DebugPreference = "Inquire"
PS C:\> Write-Debug $test
Write-Debug : Cannot convert 'System.String[]' to the type 'System.String' required by parameter 'Message'. Specified method is not supported.
At line:1 char:12
+ Write-Debug <<<<  $test
    + CategoryInfo          : InvalidArgument: (:) [Write-Debug], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.WriteDebugCommand

PS C:\>

I'm thinking this is just an unfortunate design, but hoping for a sensible explanation. Am I using Write-Debug correctly? If so, anyone have a favorite simple workaround?

4 Answers 4

18

I kept having the same problem, and none of the solutions I found above or anywhere else would work in the general case. For example, the first answer above works only because the array is an array of strings. If it's an array of anything else, that solution breaks, and Write-Debug will output the object type, and not its value as one would expect.

Finally I found a general solution: The key point is to first convert the input object to a string using the Out-String command. Once everything is a string, the above solution works. Using "Out-String -stream" improves the output alignment. Example:

PS C:\> gwmi win32_bios

SMBIOSBIOSVersion : 786F3 v01.34
Manufacturer      : Hewlett-Packard
Name              : Default System BIOS
SerialNumber      : CZC8196Q8S
Version           : HPQOEM - 20120709

PS C:\> gwmi win32_bios | ft -auto

SMBIOSBIOSVersion Manufacturer    Name                SerialNumber Version
----------------- ------------    ----                ------------ -------
786F3 v01.34      Hewlett-Packard Default System BIOS CZC8196Q8S   HPQOEM - ...

PS C:\> $DebugPreference = "Continue"
PS C:\> gwmi win32_bios | ft -auto | Write-Debug
DEBUG: Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
DEBUG: Microsoft.PowerShell.Commands.Internal.Format.GroupStartData
DEBUG: Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
DEBUG: Microsoft.PowerShell.Commands.Internal.Format.GroupEndData
PS C:\> gwmi win32_bios | ft -auto | Out-String | Write-Debug
DEBUG: SMBIOSBIOSVersion Manufacturer    Name                SerialNumber Version
----------------- ------------    ----                ------------ -------
786F3 v01.34      Hewlett-Packard Default System BIOS CZC8196Q8S   HPQOEM - ...

PS C:\> gwmi win32_bios | ft | Out-String -stream | Write-Debug
DEBUG:
DEBUG: SMBIOSBIOSVersi Manufacturer    Name            SerialNumber    Version
DEBUG: on
DEBUG: --------------- ------------    ----            ------------    -------
DEBUG: 786F3 v01.34    Hewlett-Packard Default Syst... CZC8196Q8S      HPQOEM - 201...
DEBUG:
DEBUG:PS C:\>
1
  • Are the pipeline commands gwmi win32_bios | ft | Out-String -stream | evaluated without calling the function in debug mode?
    – Dirk
    Jan 4, 2016 at 14:47
8

If you want write-debug to handle each one separately:

[string[]]$test = @("test1", "test2", "test3")
 Write-Output $test
test1
test2
test3
$DebugPreference = "Inquire"
$test | Write-Debug 

DEBUG: test1
DEBUG: test2
DEBUG: test3
2
  • I like Joel's workaround, too, but this one is much easier to read.
    – Vimes
    Jul 7, 2011 at 23:34
  • So nice and simple! +1!
    – Igor Popov
    Apr 1, 2014 at 17:25
1

Write-Debug is designed for outputting simple messages when debug preferences are set in a particular way. It takes only a string, not just anything like Write-Host does (and magically formats). You will have to format your output yourself into a single string.

You could combine Write-Host and Write-Debug if you have extra info to output before prompting the user:

if ($DebugPreference -ne 'SilentlyContinue') {
    Write-Host 'such-and-such array:' $array
}
Write-Debug 'such-and-such array dumped'

Write-Host is used because it will always write to the console host, rather than to the script's output, as Write-Output does. If you where redirecting standard output of the script to a file, Write-Output would end up in the file, while Write-Host would still be seen in the console.

You could also try doing something like this if your array is of simply enough types that an automatic call to ToString() on them (if they're not strings already) gets you what you want:

$array = 'Alice','Bob','Charlie'
Write-Debug ([String]::Join("`n", $array))
1
  • a more "PowerShell" way of doing this would be Write-Debug ($array -join "``n") (should only be one ` but had to escape it) Oct 25, 2019 at 9:50
0

Write-Debug:

Write-Debug [-Message] <string> [<CommonParameters>]

It expects a string. It is unable to convert a string array to a string as the error says. The reason why it expects a string is because it writes debug messages to the console from a script or command.

Note that Write-Output and Write-Host take an object:

Write-Output [-InputObject] <PSObject[]> [<CommonParameters>]

and

Write-Host [[-Object] <Object>] ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.