Output is in wrong order

Good morning folks,
I have this script and I have a few questions.
1st is - Looking at the output, the last line of code Write-Host…it always prints before the end of the Get-ChildItem finishes, Ive tried a few things to get it to print it at the very end. I tried putting the Get-ChildItem inside a Start-Job scriptblock but that didnt work as expected. Its a minor thing but driving me crazy. Here is the script:

$ComputerName = Read-Host -Prompt "Please enter remote computer to query"
(Get-ADComputer "$ComputerName" -Properties *).DistinguishedName.Split(',')
$CurrentOU = (Get-ADComputer "$ComputerName" -Properties *).DistinguishedName.Split(',')
$features = Invoke-Command -ComputerName $Computername -ScriptBlock {
        Date
        Systeminfo
        Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\SoftwareProtectionPlatform' -Name BackupProductKeyDefault |
        Select-Object -Property BackupProductKeyDefault
        Get-WmiObject -Class Win32_Processor
        Get-CimInstance -ClassName Win32_BIOS
        Get-CimInstance -ClassName Win32_ComputerSystem
        Get-CimInstance -ClassName Win32_OperatingSystem | select LastBootUpTime
        Get-WindowsFeature | Where-Object Installed | Format-Table Name, DisplayName, InstallState
        Get-ChildItem -Path "C:\Temp" -Recurse
                    }
        $features
        $features | Out-File -FilePath "C:\Temp\ITScript\QueryRemote\QueryRemoteLogs\$ComputerName-Info.txt"
        Add-Content -Path C:\Temp\ITScript\QueryRemote\QueryRemoteLogs\$ComputerName-Info.txt -Value $CurrentOU
        Invoke-Item C:\Temp\ITScript\QueryRemote\QueryRemoteLogs\
        Write-Host "Check log at C:\Temp\ITScript\QueryRemote\QueryRemoteLogs\$ComputerName-Info.txt for details" -ForegroundColor Yellow

The other question I have is regarding the 2nd and 3rd lines of code from the top of the script. The first is meant to print out the OU on screen, the second line is meant to stick that in a variable so I can add it to the log file at the end. Is there a way to do those 2 things without repeating the line of code, just 1 liner?

Thanks for all the help!

After looking at my 1st question…it seems that when I output
$features, the output is to much and thats what needs to be allowed to complete? Either way I changed it to just write the output to the log file, not the screen, and it solves that problem.

Great that you’ve found a solution you’re satisfied with. :+1:t4: :slightly_smiling_face:

Anyway I’d like you to point to some hopefully helpful readings:

Thanks Olaf. I loved the Snover article about Write-host.

I think for my use case, where I am building a tool that has a lot of different functions that do precise things, Im ok with Write-Host. I thought about re-writing them to use write-output/write-verbose, but for me it works better esp. that I can change the color of the text.
I also try and keep things modular, so I can re-use most of my scripts, with a little modification. At least thats the idea :slight_smile:

I was going to ask about the formatting, because I know alot of mine are not that readable, so that git article is helpful also.

-Matt