Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell storing Get-Content into variable returns no data

Tags:

powershell

xml

I have a very simple script that grabs the content from an XML file and returns it but is not working:

$DataFile = Get-ChildItem \\$Server\C$\ *.data -Name
[xml]$DataFileContent = Get-Content Microsoft.PowerShell.Core\FileSystem::\\$Server\C$\$DataFile
Write-Host $DataFileContent

This script is not returning any data on the Write-Host, but the following is returning the correct data:

$DataFile = Get-ChildItem \\$Server\C$\ *.data -Name
Get-Content Microsoft.PowerShell.Core\FileSystem::\\$Server\C$\$DataFile

Yes, the file is an xml file and the file exists. I'm sure I am missing something, but not sure what it is. Any help will be appreciated

like image 889
rjbogz Avatar asked Mar 30 '26 00:03

rjbogz


1 Answers

You cannot use Write-Host here since you're trying to display an [xml] object. Try write-output instead.

If you require Write-Host, you would need to represent the object as a string beforehand or use a property that outputs a string. For example:

[xml]$x = Get-Content y.xml
Write-Host $x.InnerXml

This may be a good idea to use with write-output, as well.

like image 141
Jacob Colvin Avatar answered Apr 01 '26 09:04

Jacob Colvin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!