0
votes

I have this piece of powershell code below which creates an individual text file in the folder C:\Users\XX\Desktop\info\ from each individual zip file in the folder C:\Users\XX\Desktop\Powershell\Zip, with the name of the text files being the name of the zip files.

Get-ChildItem -Path "C:\Users\XX\Desktop\Powershell\Zip" -Recurse -exclude '*.info' | ForEach { [System.IO.File]::WriteAllText("C:\Users\XX\Desktop\info\"+ $_.Name + ".txt", $_.FullName)} 

ontop of that I have the script below which gets the last modified date for the zip files

$path = 'C:\Users\XX\Desktop\Powershell\Zip'

$files = Get-ChildItem $path -Recurse -excluse '*.info' 

foreach($file in $files){

$file.lastwritetime

and also this command that gets the computer name

{
(Get-WmiObject Win32_Computersystem).name 
}

All these will be in one script, but I need the outputs of the 2nd and 3rd section of the script to append to the text file created in the first section of the script, appending to the appropriate file.

I have tried a couple of commands, the main one being [System.IO.File]::AppendAllText, but I cant seem to get anywhere with this.

Any ideas on the right way I should be doing this?

Thankyou.

1

1 Answers

1
votes

You can try this :

$path = 'C:\Users\XX\Desktop\Powershell\Zip'

$files = Get-ChildItem $path -Recurse -Exclude '*.info' 

$ComputerName = (Get-WmiObject Win32_Computersystem).name

foreach($file in $files) {

    $OutputFilePath = "C:\Users\XX\Desktop\info\"+ $file.Name + ".txt"

    [System.IO.File]::WriteAllText($OutputFilePath, $file.FullName)

    $file.lastwritetime | Add-Content $OutputFilePath

    $ComputerName | Add-Content $OutputFilePath
}