# Need help with simpley powershell code

**URL:** <https://forums.powershell.org/t/need-help-with-simpley-powershell-code/15107>\
**Category:** PowerShell Help\
**Created:** [October 19, 2020, 5:04am UTC](https://forums.powershell.org/t/need-help-with-simpley-powershell-code/15107 "2020-10-19T05:04:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![query](https://avatars.discourse-cdn.com/v4/letter/q/3ab097/32.png) [@query](https://forums.powershell.org/u/query)\
**Post date:** [October 19, 2020, 5:04am UTC](https://forums.powershell.org/t/need-help-with-simpley-powershell-code/15107/1 "2020-10-19T05:04:24Z")

</div>

Hi @all

I don’t know very much powershell. I have a simple question:

I have e environment with 40 virtual Windows Servers (2012R2, 2016 and 2019). Now I woll use a Powershellscript on one Server (Domaincontroller) who will put all installed updates on all servers. Limited to a period. An I will use a file (.txt or .csv) to set the servernames who I will check with this script.

Example: I will set the following in the script and then i can see all installed updates from a period only for this servers:

[Server1.domain.com](http://Server1.domain.com)  
[Server2.domain.com](http://Server2.domain.com)  
[Server3.domain.com](http://Server3.domain.com)

At the moment I have the following code, but I cannot set a period or use a file to restrict the scanned servers:

```
Invoke-Command -ComputerName (Get-ADComputer -Filter *).Name {Get-HotFix} -ErrorAction SilentlyContinue | Select-Object PSComputername, HotfixID, InstalledOn | Out-GridView
```

Any ideas or help?

Thank you

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 19, 2020, 6:42am UTC](https://forums.powershell.org/t/need-help-with-simpley-powershell-code/15107/2 "2020-10-19T06:42:10Z")

</div>

You really should not be using a domain controller for this. It should be done from an admin workstation/server.

You can use Get-Content to read the text file and then create a loop to get the hotfix information for each computer. You can pipe the output of Get-HotFix to Where-Object to filter the dates.

```
$computers = Get-Content c:\temp\computers.txt

foreach ($computer in $computers) {

    Get-HotFix -ComputerName $computer | 
        Where-Object {$_.InstalledOn -gt (Get-Date '01/07/2020') -and $_.InstalledOn -lt (Get-Date '19/10/2020')} | 
            Select-Object PSComputername, HotfixID, InstalledOn | Out-GridView

}
```

&nbsp;

---

<div class="post-metadata">

**Author:** ![query71](https://avatars.discourse-cdn.com/v4/letter/q/57b2e6/32.png) [@query71](https://forums.powershell.org/u/query71)\
**Post date:** [October 20, 2020, 12:16am UTC](https://forums.powershell.org/t/need-help-with-simpley-powershell-code/15107/3 "2020-10-20T00:16:24Z")

</div>

Hi @Matt Bloomfield

Thanks for your help. It works. But I have a littel change/question. I will output the result in a .txt file. I modified the code with “output-file”:

$computers = Get-Content c:\temp\computers.txt

foreach ($computer in $computers) {

Get-HotFix -ComputerName $computer |  
Where-Object {$_.InstalledOn -gt (Get-Date ‘06/07/2020’) -and $_.InstalledOn -lt (Get-Date ‘20/10/2020’)} |  
Select-Object PSComputername, Description, HotfixID, InstalledBy, InstalledOn | Out-File -FilePath C:\Temp\ServerupdatesNew.txt

}

The problem is, at the end the .txt-file is empty. But when I add only one server in the comptuers.txt, then it works. Is there a limit? Or do I need a other code?

Thank you.

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 20, 2020, 12:30am UTC](https://forums.powershell.org/t/need-help-with-simpley-powershell-code/15107/4 "2020-10-20T00:30:10Z")

</div>

You are overwriting the text file each time the loop runs. Look at the help for Out-File and you should be able to work out how to append data to a file rather than overwriting it each time.

---

<div class="post-metadata">

**Author:** ![query71](https://avatars.discourse-cdn.com/v4/letter/q/57b2e6/32.png) [@query71](https://forums.powershell.org/u/query71)\
**Post date:** [October 20, 2020, 1:22am UTC](https://forums.powershell.org/t/need-help-with-simpley-powershell-code/15107/5 "2020-10-20T01:22:10Z")

</div>

Yeah, great. I add the “-append” an now it works. Thank you very much 🙂

---

<div class="post-metadata">

**Author:** ![query71](https://avatars.discourse-cdn.com/v4/letter/q/57b2e6/32.png) [@query71](https://forums.powershell.org/u/query71)\
**Post date:** [October 20, 2020, 1:49am UTC](https://forums.powershell.org/t/need-help-with-simpley-powershell-code/15107/6 "2020-10-20T01:49:21Z")

</div>

But one more question. What I need to change, when I will an other Date-Format? At the moment it is the following: MM:DD:YYYY

I will change the input and output to DD:MM:YYYY

---

<div class="post-metadata">

**Author:** ![matt-bloomfield](https://avatars.discourse-cdn.com/v4/letter/m/dec6dc/32.png) [@matt-bloomfield](https://forums.powershell.org/u/matt-bloomfield)\
**Post date:** [October 20, 2020, 5:44am UTC](https://forums.powershell.org/t/need-help-with-simpley-powershell-code/15107/7 "2020-10-20T05:44:46Z")

</div>

Check the help for Get-Date, you can use the -Format parameter to get the desired format.

Get-Date '20/10/2020' -Format 'MM:dd:yyyy'

---

<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:29pm UTC](https://forums.powershell.org/t/need-help-with-simpley-powershell-code/15107/8 "2024-05-16T20:29:12Z")

</div>


