# Format from a Variable

**URL:** <https://forums.powershell.org/t/format-from-a-variable/2422>\
**Category:** PowerShell Help\
**Created:** [April 5, 2014, 4:09am UTC](https://forums.powershell.org/t/format-from-a-variable/2422 "2014-04-05T04:09:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![david-deherrera](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@david-deherrera](https://forums.powershell.org/u/david-deherrera)\
**Post date:** [April 5, 2014, 4:09am UTC](https://forums.powershell.org/t/format-from-a-variable/2422/1 "2014-04-05T04:09:41Z")

</div>

I am trying to format my data once I have saved it in a variable. But I am unable to figure out a way of doing this. Here is my process:

```
$strComputers = "Computer1", "Computer2"
$FailOutput = "Location, State `n"
Foreach ($strcomputer in $strComputers)
{
    If (!(Test-Connection -ComputerName $strComputer -Count 1 -Quiet))
    {
        $FailOutput += "$($strComputer), Is not online or exsit. `n"
    }
}
$FailOutput
```

And here is what I get:  
**Location, State  
Computer1, Is not online or exsit.  
Computer2, Is not online or exsit.**

But I would like if the computer names are not the same amount of characters, I would like everything tabbed or in a neat table.

How would I go about doing this?

Thank you in advance.  
David D.

---

<div class="post-metadata">

**Author:** ![donj](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/donj/32/137_2.png) [@donj](https://forums.powershell.org/u/donj)\
**Post date:** [April 5, 2014, 5:23am UTC](https://forums.powershell.org/t/format-from-a-variable/2422/2 "2014-04-05T05:23:26Z")

</div>

If you want PowerShell to do that for you, then you need to create a custom object instead of just outputting text.

`
$properties = @{'ComputerName'=$strComputer;
                'Status'=$whatever}
New-Object -Type PSObject -Prop $properties
`

You could pipe the new object to a text file, to Export-CSV, or whatever. This is the fundamental unit of work in Windows PowerShell, so mastering object-based output is pretty much key to getting the shell to do anything for you. Check out my “Learn PowerShell Toolmaking in a Month of Lunches” book - it’s based around this entire concept.

---

<div class="post-metadata">

**Author:** ![bert-nieuwenampsen](https://avatars.discourse-cdn.com/v4/letter/b/dc4da7/32.png) [@bert-nieuwenampsen](https://forums.powershell.org/u/bert-nieuwenampsen)\
**Post date:** [April 5, 2014, 9:01pm UTC](https://forums.powershell.org/t/format-from-a-variable/2422/3 "2014-04-05T21:01:49Z")

</div>

`
$strcomputers = "computer1", "computer2"
$failoutput = "location, state `n"
`

`foreach ($strcomputer in $strcomputers)
{
$test = “$($strcomputer), is online. n" if (!(test-connection -computername $strcomputer -count 1 -quiet)) { $test = "$($strcomputer), is not online or exsist. n”
}
$failoutput += $test
}
$failoutput
`

---

<div class="post-metadata">

**Author:** ![david-deherrera](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@david-deherrera](https://forums.powershell.org/u/david-deherrera)\
**Post date:** [April 7, 2014, 12:41am UTC](https://forums.powershell.org/t/format-from-a-variable/2422/4 "2014-04-07T00:41:57Z")

</div>

Don, Picked up my book. I thought I knew PowerShell pretty good, but now I have been put in my place. ha

Thank you again, I will start my read.  
David D.

---

<div class="post-metadata">

**Author:** ![david-deherrera](https://avatars.discourse-cdn.com/v4/letter/d/eada6e/32.png) [@david-deherrera](https://forums.powershell.org/u/david-deherrera)\
**Post date:** [April 7, 2014, 10:37pm UTC](https://forums.powershell.org/t/format-from-a-variable/2422/5 "2014-04-07T22:37:50Z")

</div>

Just FYI, here is my final sample script, and works like a charm. Thanks again Don for the proper push.

```auto
$strComputers = "Computer1", "Computer2"
Foreach ($strcomputer in $strComputers)
{
    If (!(Test-Connection -ComputerName $strComputer -Count 1 -Quiet))
    {
        $whatever = "Not Online"
        $properties = @{'ComputerName'=$strComputer; 'Status'=$whatever}
        New-Object -Type PSObject -Prop $properties
    }
}

```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:52pm UTC](https://forums.powershell.org/t/format-from-a-variable/2422/6 "2024-05-16T20:52:13Z")

</div>


