# PowerShell ignoring lines?

**URL:** <https://forums.powershell.org/t/powershell-ignoring-lines/22838>\
**Category:** PowerShell Help\
**Created:** [September 4, 2023, 10:31pm UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838 "2023-09-04T22:31:25Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![shanebagel](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@shanebagel](https://forums.powershell.org/u/shanebagel)\
**Post date:** [September 4, 2023, 10:31pm UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/1 "2023-09-04T22:31:25Z")

</div>

I have a script that is dead simple. It gets system info. But when I execute it it’s only running like 3 of the lines and not outputting anything else to the screen?

---

<div class="post-metadata">

**Author:** ![shanebagel](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@shanebagel](https://forums.powershell.org/u/shanebagel)\
**Post date:** [September 4, 2023, 10:32pm UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/2 "2023-09-04T22:32:03Z")

</div>

> <https://github.com/shanebagel/Fun-PowerShell-Scripts/blob/main/SHMachineInfo/Functions/Get-CoreInfo.ps1>

This is the code

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 5, 2023, 5:22pm UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/3 "2023-09-05T17:22:22Z")

</div>

Do you really need this:

> [@shanebagel](#):
>
> `if ($PSVersionTable.PSVersion.Major -ge 3) {`

?  
All Windows versions coming with less than PowerShell version 3.0 are out of support. 😉

Regardless of that is your code a little cumbersome, redundand and confusing. You query every individual computer 6 times. 3 times of wich you’re querying the same CIM class. That’s very inefficient.

I’d start with something like this:

```auto
function Get-CoreInfo {
    Param(
        [Parameter(
            ValueFromPipeline = $true,
            ValueFromPipelineByPropertyName = $true
        )]
        [String[]]$ComputerName = $Env:COMPUTERNAME
    )
    process {
        foreach ($Computer in $ComputerName) {
            $CimSession = New-CimSession -ComputerName $Computer

            $ComputerSystem = 
                Get-CimInstance -ClassName CIM_ComputerSystem -CimSession $CimSession
            $OperatingSystem = 
                Get-CimInstance -ClassName CIM_OperatingSystem -CimSession $CimSession
            $BIOS = 
                Get-CimInstance -ClassName CIM_BIOSElement -CimSession $CimSession
    
            [PSCustomObject]@{
                ComputerName = $ComputerSystem.Name
                Manufacturer = $ComputerSystem.Manufacturer
                Model = $ComputerSystem.Model
                SerialNumber = $BIOS.SerialNumber
                OperatingSystem = $OperatingSystem.Caption
                Version = $OperatingSystem.Version
                SystemArchitecture = $OperatingSystem.OSArchitecture
                BIOSName = $BIOS.Name
                BIOSManufacturer = $BIOS.Manufacturer
                BIOSVersion = $BIOS.Version
            }
        }
    }
}

```

This way you can run this function without any parameter for the local computer. With one or more computer names as parameters or computer names passed by pipeline.

```auto
Get-CoreInfo

Get-CoreInfo -ComputerName RemoteComputer_01, RemoteComputer_02

'RemoteComputer_01', 'RemoteComputer_02' | 
    Get-CoreInfo | 
        Format-Table -AutoSize

```

---

<div class="post-metadata">

**Author:** ![shanebagel](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@shanebagel](https://forums.powershell.org/u/shanebagel)\
**Post date:** [September 5, 2023, 6:09pm UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/4 "2023-09-05T18:09:31Z")

</div>

Oh wow - so completely remove all entries for WMI ?

The idea is I want to have a **ton** of little functions, all of which do something on their own, they all get information.

Example- get-netinfo or get-storageinfo

> <https://github.com/shanebagel/Fun-PowerShell-Scripts/tree/main/SHMachineInfo>
>
> //github.com/shanebagel/Fun-PowerShell-Scripts/tree/main/SHMachineInfo

But I want to package it as a module, import all those functions into one module file, then create ONE function that calls each of these ‘little’ functions individually - like a really comprehensive version of GET-COMPUTERINFO - except with like all computer info possible

Do you have any recommendations on how to do that? 🙂

Thanks for your help Olaf

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 5, 2023, 6:43pm UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/5 "2023-09-05T18:43:58Z")

</div>

> [@shanebagel](#):
>
> Oh wow - so completely remove all entries for WMI ?

Yes. It is not supported anymore in PowerShell v 7.x anyway. 🤷🏼‍♂️

> [@shanebagel](#):
>
> But I want to package it as a module,

That’s a nice idea. Especially when you share your code with colleagues. 👍🏼

> [@shanebagel](#):
>
> import all those functions into one module file,

I wouldn’t do that. IMO that would make it harder to maintain. I like to keep every single function in its own file with the same name like the function. ☝🏼

> [@shanebagel](#):
>
> then create ONE function that calls each of these ‘little’ functions individually

Hmmm … I’d expect that to be a messy output since the information provided by those functions are really different from each other. 🤷🏼‍♂️

> [@shanebagel](#):
>
> like a really comprehensive version of GET-COMPUTERINFO - except with like all computer info possible

Whatfor? Do you have a real world use case for such an info overkill? 😉

> [@shanebagel](#):
>
> Do you have any recommendations on how to do that? 🙂

What exactly?

… unsing modules ingeneral?

> **[about Modules - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules?view=powershell-7.3)**
>
> Explains how to install, import, and use PowerShell modules.

… creating modules in general?

> **[How to Write a PowerShell Script Module - PowerShell](https://learn.microsoft.com/en-us/powershell/scripting/developer/module/how-to-write-a-powershell-script-module?view=powershell-7.3)**
>
> How to Write a PowerShell Script Module

… my general recommendation would be: before you start re-inventing the wheel again by writing your own code, search online for examples of code for the task you want to accomplish. For the vast majority of the cases you’re not the very first one with a given task and there are probably code snippets, scripts or modules out there you can use right away. And maybe they are even better than anything you could come up with.

A great place to start your search would be the  
[https://www.powershellgallery.com/](https://www.powershellgallery.com/)  
or

> **[GitHub: Let’s build from here](https://github.com/)**
>
> GitHub is where over 100 million developers shape the future of software, together. Contribute to the open source community, manage your Git repositories, review code like a pro, track bugs and fea...

or

> **[Stack Overflow - Where Developers Learn, Share, & Build Careers](https://stackoverflow.com/)**
>
> Stack Overflow | The World’s Largest Online Community for Developers

When you write your own code you may follow the

> **[GitHub - PoshCode/PowerShellPracticeAndStyle: The Unofficial PowerShell Best...](https://github.com/PoshCode/PowerShellPracticeAndStyle)**
>
> The Unofficial PowerShell Best Practices and Style Guide - GitHub - PoshCode/PowerShellPracticeAndStyle: The Unofficial PowerShell Best Practices and Style Guide

And please … always read the help for the cmdlets you’re about to use COMPLETELY INCLUDING THE EXAMPLES to learn how to use them. ☝🏼

Have a lot of fun!  
🤟🏼

---

<div class="post-metadata">

**Author:** ![shanebagel](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@shanebagel](https://forums.powershell.org/u/shanebagel)\
**Post date:** [September 5, 2023, 7:02pm UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/6 "2023-09-05T19:02:11Z")

</div>

Awesome man - thank you for your help Olaf as always!

---

<div class="post-metadata">

**Author:** ![shanebagel](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@shanebagel](https://forums.powershell.org/u/shanebagel)\
**Post date:** [September 6, 2023, 3:09am UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/7 "2023-09-06T03:09:58Z")

</div>

Hey Olaf - I had one more question that has been bugging me and I can’t find a clear answer on?

All the PWSH books really drive this point home that you should always be outputting data to Write-Output and to avoid Write-Host like the plague lol

- Is there ever really a reason to use Write-Host?
- Or is it best practice to just always output using Write-Output even in the case of returning strings or just showing output to the end user who’s running the script?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 6, 2023, 6:26am UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/8 "2023-09-06T06:26:59Z")

</div>

> [@shanebagel](#):
>
> Is there ever really a reason to use Write-Host?

If you want to output something explicitly to the console - yes. But for most of the use cases it is used out there there are usually better options like `Write-Debug` or `Write-Verbose`. Implemented correctly you can switch those messages on if needed and omit it by default.

> [@shanebagel](#):
>
> Or is it best practice to just always output using Write-Output

There is barely the need to explicitly use `Write-Output` since everything you _leave_ or _drop_ from your code becomes implicitly part of the output stream.

---

<div class="post-metadata">

**Author:** ![shanebagel](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@shanebagel](https://forums.powershell.org/u/shanebagel)\
**Post date:** [September 6, 2023, 1:18pm UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/9 "2023-09-06T13:18:13Z")

</div>

You included the pscustomobject in your implementation on my script to create an object

Is that best practice to do that for commands that ‘get’ something?  
Would just simply calling the objects properties suffice?

Ex

```auto
            $ComputerSystem = 
                Get-CimInstance -ClassName CIM_ComputerSystem -CimSession $CimSession

```

Wouldn’t I be able to just call $ComputerSystem and format it with Select-Object by making a custom property? Or is that bad practice and I should always use the pscustomobject with commands that ‘Get’ something?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [September 6, 2023, 1:29pm UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/10 "2023-09-06T13:29:11Z")

</div>

> [@shanebagel](#):
>
> Is that best practice to do that for commands that ‘get’ something?

It is IMO the best way to combine results from more than one query in one output.

> [@shanebagel](#):
>
> Wouldn’t I be able to just call $ComputerSystem and format it with Select-Object by making a custom property?

Of course. If you want to output _ONLY_ the properties for the CIM class `CIM_ComputerSystem` separately you can use a simple `Select-Object`. In general it is totally up to you how you use PowerShell for your particular needs. 🤷🏼‍♂️

---

<div class="post-metadata">

**Author:** ![shanebagel](https://avatars.discourse-cdn.com/v4/letter/s/57b2e6/32.png) [@shanebagel](https://forums.powershell.org/u/shanebagel)\
**Post date:** [September 6, 2023, 1:35pm UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/11 "2023-09-06T13:35:40Z")

</div>

Thank you! I will try to use New-Object more in my scripts!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 7, 2024, 6:36am UTC](https://forums.powershell.org/t/powershell-ignoring-lines/22838/12 "2024-02-07T06:36:18Z")

</div>

Closed on February 7, 2024, 6:36am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
