Powershell: loop from text file line by line + more

  • Grab every file in specified folder/directory
  • Read each file line by line
  • Create / append new file from output
  • After file has been created join / merge to file
  • Merge header & footer Files plus new file into one  large file
#Folder of something
#grab the flies
Get-ChildItem C:\Users\Desktop\powershell\vmat\itnetlabsVAMT\ -Recurse |
 Where {$_ -IS [IO.FileInfo]} |
#GET CONTENT $_ means each line from get content
gc $_ | ForEach-Object {

 #Shows what you grabbed
write-host ($_)

######################
#write the output to a file
write-output ' <Computer Name="'($(_))'"
 OSVersion="" OSEdition="" NetworkType="Domain" NetworkName="
" Id="bac224">
 <MacAddresses />
<Products>
<Product>
<Sku Id="00000x00-0000-0000-0000-0x00" Name=""
ApplicationId="5c3f16059f" Description="" />
<LicenseInfo LicenseStatus="Unknown" LicenseStatusReason="0"
 GracePeriodRemaining="0" GenuineStatus="Unknown"
TokenEnabled="false" LicenseIsAddon="false" />
<Version />
<PartialProductKey />
<ProductKeyId />
<KeyType>Unknown</KeyType>
<InstallationId />
<ConfirmationId />
<LastErrorCode>0</LastErrorCode>
<LastActionState />
<LastUpdateTime>2012-03-08T10:39:24.0468d-06:00</LastUpdateTime>
<LicenseFamily />
</Product>
</Products>
</Computer>
</Computers>'
} | Out-File $($pathName + "_DUMP.txt")
#ABOVE LINE PIPe OUTPUT TO TEXT FILE
#MAKES A TEXT FILE
New-Item -ItemType file "big.cli"
#merges the files
$file1 = Get-Content "header.txt"
$file2 = Get-Content $($pathName + "_DUMP.txt")
$file3 = Get-Content "Footer.txt"
Add-Content "big.cli" $file1
Add-Content "big.cli" $file2
Add-Content "big.cli" $file3

<code></pre></code>
<code>

[\powershell]

share with friends

Share to Google Buzz
Share to Google Plus
Share to LiveJournal
Share to Odnoklassniki

One Response to Powershell: loop from text file line by line + more