How can I add the header to a text file? - powershell-2.0

How can I add the header to a top of text file using PowerShell? I have tried using the below code like:
Add-Content -Path E:\BHS_output_1.txt -Value (Get-Content "E:\header.txt")

Like this:
$textfile = 'E:\BHS_output_1.txt'
$headerfile = 'E:\header.txt'
$(Get-Content $headerfile; Get-Content $textfile) | Set-Content $textfile
Read the contents of header and text file after each other in a subexpression, then write the entire output back to the text file. The subexpression is required, so that reading from the text file is completed before writing starts, otherwise you'd get an access conflict.

Related

Want to recursively list full path of all files in a directory structure and pipe into text file without wordwrap

I want to get a listing of the full path of every file in a multi-level directory structure, and put that information into a text file - BUT, I don't want long path's to be word-wrapped - I want to have the full path of each line on a single line in the text regardless of how long that line is.
I started out with this command:
Get-ChildItem -Recurse | ForEach-Object { Write-Host $_.FullName.ToString() }
I then have tried various means to try and redirect this output to a text file. First I tried this (but it didn't work):
Get-ChildItem -Recurse | ForEach-Object { Write-Host $_.FullName.ToString() } | Out-File C:\temp\out.txt
Next, I tried just piping all of the StdErr, StdOut etc to the text file thus:
Get-ChildItem -Recurse | ForEach-Object { Write-Host $_.FullName.ToString() } *> C:\temp\out.txt
This last command got close - but still the longer path's are wrapped onto the next line. I have been trying all sorts of things to try and stop this (wordwrap) from happening. Can anyone tell me how to get the output from the above command to go into a text file with no word-wraps (even) for very long path's?
thanks heaps,
David. :-)

Converting text output to object using powershell

I am trying to schedule a list of URL's in maintenance mode in SCOM 2007 using powershell. I am trying to get the list of URLs display name from a text file and trying to pass as input to below command.However it's not working. Can some body help how to pass the display name in text file as input
$URLStuff = Get-Content C:\Display.txt
$URLWatcher = (Get-MonitoringClass -name Microsoft.SystemCenter.WebApplication.Perspective) |
Get-MonitoringObject | where {$_.DisplayName -eq $URLStuff}
get-content is returning you an array of string objects, one per line found in the file. You need to turn your where-object around to search that array for the DisplayName of each object found from SCOM.
$URLWatcher = (Get-MonitoringClass -name Microsoft.SystemCenter.WebApplication.Perspective) |
Get-MonitoringObject | where {$URLStuff -contains $_.DisplayName}
I'm assuming that you've already verified that DisplayName does contain the data you're looking for and will match the contents of Display.txt.

I cannot get Powershell 2.0 to move a file from one path to another

I need to make changes to to the following Powershell script, but am having a dickens of a time getting the resulting files to write to a different path...let's call it $destPath.
Consider:
Get-ChildItem $sourcePath | % { [system.io.file]::Move($_.fullname, ($_.FullName -replace '\[|\]|-|,|\(|\)', '') ) }
Based on my understanding of move syntax, $_.fullname is my original file, and $_.FullName -replace... is the NEW filename. Hoever, when I try to use $destPath.FullName -replace I get an error that an empty filename is not legal. Obviously, Powershell is not recognizing that as a valid pathname for the move command.
What am I missing?
Since you did not mention $destPath in context - maybe you did not define the variable $destPath at all? It is quite unlikely but just trying to narrow things down.
Another way to make this work:
Rename child-items first at original location. Then move them.
get-childitem *.txt | rename-item -newname {$_.name -replace 'o','O'}
get-childitem *.txt | % {move-item -path $_.fullname -destination .\Test-files}
But I prefer your one liner:)

Update timestamps of files in windows using a text document for reference

I need a way to update timestamps of files to the current date using a text document as a pointer to the files.
For example:
Directory A structure:
A:\Level 1\Level 2\somefile1
A:\Level 1\somefile2
A:\Level 1\somefile3
A:\somefile4
Text file (FilePointer.txt) contents:
A:\Level 1\Level 2\somefile1
A:\Level 1\somefile2
A:\Level 1\somefile3
A:\somefile4
Is there a way to parse the FilePointer.txt file to obtain file locations, and feed that to something that can update the timestamp for those files? Using powershell? Other methods?
That should be pretty easy with PowerShell:
$timestamp = Get-Date # now
Get-Content filepointer.txt | Get-Item | Foreach {$_.LastWriteTime = $timestamp}
There's a bit simpler approach outlined here. It uses the PowerShell Community Extensions Set-FileTime command e.g.:
Get-Content filepointer.txt | Get-Item | Set-FileTime
Or using aliases:
gc filepointer.txt | gi | touch

Importing CSV from a variable instead of file?

I have a command that formats it's output in the form of CSV. I have a list of machine this command will run against using a foreach loop. in the below example $serverlist is automatically generated with an AD Query.
foreach ($server in $serverlist) {
$outputlist = mycommand
}
what I would like to do is somehow end up with objects from the resulting CSV so I can then only select certain objects for a report. However the only way I can see to do this is using import-csv, which only seems to want to work with files and not variable: ie.
Import-Csv output.csv | ft "HostName","TaskName" |
Where-object {$_.TaskName -eq 'Blah'}
I'd like to be able to have import-csv $outputlist instead. doing this causes import-csv to have a hissyfit :)
Can anyone point me in the right direction on how to achieve this?
Cheers
The command you want is called ConvertFrom-CSV. The syntax is shown below.
NAME
ConvertFrom-CSV
SYNOPSIS
Converts object properties in comma-separated value (CSV) format into CSV
versions of the original objects.
SYNTAX
ConvertFrom-CSV [[-Delimiter] <char>] [-InputObject] <PSObject[]> [-Header <string[]>] [<CommonParameters>]
ConvertFrom-CSV -UseCulture [-InputObject] <PSObject[]> [-Header <string[]>] [<CommonParameters>]

Resources