Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Newbie - Output to a text file problem.

Reply
 
Old 10-11-2007   #1 (permalink)
Kryten


 
 

Newbie - Output to a text file problem.

I have a feeling I'm going to look silly for asking this but it's got
me stumped!

I wrote this script:-


# get user input for numeric values for
loop,shelf,card,unitstart,unitend.
$loop = Read-Host "Please enter the loop number:"
$shelf = Read-Host "Please enter the shelf:"
$card = Read-Host "please enter the card:"
[int]$unitstart = Read-Host "Please enter the start unit:"
[int]$unitend = Read-Host "Please enter the final unit:"
#
# Start the loop to print the 1st value of unit then increase
# until value of unitend is reached
#
while ($unitstart -le $unitend)
{
[string]$tn = $loop + " " + $shelf + " " + $card + " " + $unitstart
write-output $tn
$unitstart++
}

This writes to the shell window a series of numbers. Eg if you enter
148 for Loop, 0 for Shelf, 4 for Card and 0 for startunit then 31 for
endunit; you will see what I mean.

How can I get this to write to a text file instead (or as well as) the
shell window. So far the best I was able to do was get only the last
line of output written into the text file! - Like I say I AM a
beginner!

I suppose that the problem is getting the products of the "if"
interations written into notepad on a new line for each iteration.

Sorry if this is a daft thing to be stuck on!

-Stuart


My System SpecsSystem Spec
Old 10-11-2007   #2 (permalink)
Kiron


 
 

Re: Newbie - Output to a text file problem.

# you can use add-content,
# change the encoding to your choice
# use PowerShell's variable expansion to form the string

while ($unitstart -le $unitend)
{
add-content output.txt "$loop $shelf $card $unitstart" -encoding ASCII
#write-output $tn
$unitstart++
}

# ...or set-content

while ($unitstart -le $unitend)
{
[string[]]$tn += "$loop $shelf $card $unitstart"
#write-output $tn
$unitstart++
}
set-content output.txt $tn -encoding ASCII
--
Kiron
My System SpecsSystem Spec
Old 10-11-2007   #3 (permalink)
Brandon Shell [MVP]


 
 

Re: Newbie - Output to a text file problem.

If I understand you correctly you have three options

1) Use out-file -append
2) Wrap the while loop in @()
3) Save the output to a variable and at the end write that to a file

####### 1 ##########
while ($unitstart -le $unitend)
{
[string]$tn = $loop + " " + $shelf + " " + $card + " " + $unitstart
write-output $tn | out-file $file -append
$unitstart++
}


##################

####### 2 ##########
@(while ($unitstart -le $unitend)
{
[string]$tn = $loop + " " + $shelf + " " + $card + " " + $unitstart
write-output $tn
$unitstart++
}) | out-file $file


###################


####### 3 ##########
$results = @()
while ($unitstart -le $unitend)
{
[string]$tn = $loop + " " + $shelf + " " + $card + " " + $unitstart
$results += write-output $tn
$unitstart++
}
$results | out-file $file

###################
Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

K> I have a feeling I'm going to look silly for asking this but it's got
K> me stumped!
K>
K> I wrote this script:-
K>
K> # get user input for numeric values for
K> loop,shelf,card,unitstart,unitend.
K> $loop = Read-Host "Please enter the loop number:"
K> $shelf = Read-Host "Please enter the shelf:"
K> $card = Read-Host "please enter the card:"
K> [int]$unitstart = Read-Host "Please enter the start unit:"
K> [int]$unitend = Read-Host "Please enter the final unit:"
K> #
K> # Start the loop to print the 1st value of unit then increase
K> # until value of unitend is reached
K> #
K> while ($unitstart -le $unitend)
K> {
K> [string]$tn = $loop + " " + $shelf + " " + $card + " " + $unitstart
K> write-output $tn
K> $unitstart++
K> }
K> This writes to the shell window a series of numbers. Eg if you enter
K> 148 for Loop, 0 for Shelf, 4 for Card and 0 for startunit then 31 for
K> endunit; you will see what I mean.
K>
K> How can I get this to write to a text file instead (or as well as)
K> the shell window. So far the best I was able to do was get only the
K> last line of output written into the text file! - Like I say I AM a
K> beginner!
K>
K> I suppose that the problem is getting the products of the "if"
K> interations written into notepad on a new line for each iteration.
K>
K> Sorry if this is a daft thing to be stuck on!
K>
K> -Stuart
K>


My System SpecsSystem Spec
Old 10-11-2007   #4 (permalink)
Kiron


 
 

Re: Newbie - Output to a text file problem.

Sorry, missed the output to console as well as to a file:

# with add-content
while ($unitstart -le $unitend)
{
add-content output.txt "$loop $shelf $card $unitstart"
write-output "$loop $shelf $card $unitstart"
$unitstart++
}

# with set-content
while ($unitstart -le $unitend)
{
[string[]]$tn += "$loop $shelf $card $unitstart"
$unitstart++
}
set-content output.txt $tn
write-output $tn

--
Kiron
My System SpecsSystem Spec
Old 10-11-2007   #5 (permalink)
Jeff


 
 

Re: Newbie - Output to a text file problem.

On Oct 12, 12:14 am, Kryten <Kryte...@xxxxxx> wrote:
Quote:

> I have a feeling I'm going to look silly for asking this but it's got
> me stumped!
>
> I wrote this script:-
>
> # get user input for numeric values for
> loop,shelf,card,unitstart,unitend.
> $loop = Read-Host "Please enter the loop number:"
> $shelf = Read-Host "Please enter the shelf:"
> $card = Read-Host "please enter the card:"
> [int]$unitstart = Read-Host "Please enter the start unit:"
> [int]$unitend = Read-Host "Please enter the final unit:"
> #
> # Start the loop to print the 1st value of unit then increase
> # until value of unitend is reached
> #
> while ($unitstart -le $unitend)
> {
> [string]$tn = $loop + " " + $shelf + " " + $card + " " + $unitstart
> write-output $tn
> $unitstart++
>
> }
>
> This writes to the shell window a series of numbers. Eg if you enter
> 148 for Loop, 0 for Shelf, 4 for Card and 0 for startunit then 31 for
> endunit; you will see what I mean.
>
> How can I get this to write to a text file instead (or as well as) the
> shell window. So far the best I was able to do was get only the last
> line of output written into the text file! - Like I say I AM a
> beginner!
>
> I suppose that the problem is getting the products of the "if"
> interations written into notepad on a new line for each iteration.
>
> Sorry if this is a daft thing to be stuck on!
>
> -Stuart
If you want to get the output in a file as well as the console without
changing your script, you can use the Tee-Object Cmdlet:

..\script.ps1 | Tee-Object -FilePath "out.txt"

I hope this helps.

Jeff

My System SpecsSystem Spec
Old 10-11-2007   #6 (permalink)
Kryten


 
 

Re: Newbie - Output to a text file problem.

Hi Kiron,

Thanks for the suggestions - tried them both and they both work
perfectly well.
Just wish I could have thought of them for myself.
Many thanks!

Stuart

My System SpecsSystem Spec
Old 10-11-2007   #7 (permalink)
Kryten


 
 

Re: Newbie - Output to a text file problem.

Hi Brandon,

Thanks for the comprehensive reply.

I've tried the out-file - append method and it work just great,
haven't tried the others yet but I'm sure they will work too.

Brandon, in the last week or two since I fell in love with PowerShell
I've began reading "PowerShell in Action" and started
actually writing some scripts and reading everything I can about it.
What I would like to ask you is "how does one best become
proficient in PowerShell?" How did you learn it? If you see what I
mean.

Keep up the good work!

Thanks,
Stuart

My System SpecsSystem Spec
Old 10-11-2007   #8 (permalink)
Kryten


 
 

Re: Newbie - Output to a text file problem.

Hi Jeff,

Thanks for your input. "Tee-Object" that seems like a really useful
command
I'll give it a try.

Thanks,

Stuart

My System SpecsSystem Spec
Old 10-11-2007   #9 (permalink)
Brandon Shell [MVP]


 
 

Re: Newbie - Output to a text file problem.

I believe the best way to learn something is to use it and while that may
sound obvious it absolutely true.

That said everyone had different learning styles... mine is trial by fire

My first VBScript was close to 1200 lines and my first Powershell Script
was close to 720.

My point is that for me, I just went all out full board and picked that seemed
to be an impossible task for someone that knew next to nothing about .NET
and even less about Powershell. I think developer have a headstart when it
comes to Powershell. It is an Admin shell, and there is no dout about that,
but it was built around a VERY dev centric product .NET.

I think the NG, Blogs, and Books are a great start.

Brandon Shell
---------------
Blog: http://www.bsonposh.com/
PSH Scripts Project: www.codeplex.com/psobject

K> Hi Brandon,
K>
K> Thanks for the comprehensive reply.
K>
K> I've tried the out-file - append method and it work just great,
K> haven't tried the others yet but I'm sure they will work too.
K>
K> Brandon, in the last week or two since I fell in love with PowerShell
K> I've began reading "PowerShell in Action" and started
K> actually writing some scripts and reading everything I can about it.
K> What I would like to ask you is "how does one best become
K> proficient in PowerShell?" How did you learn it? If you see what I
K> mean.
K> Keep up the good work!
K>
K> Thanks,
K> Stuart


My System SpecsSystem Spec
Old 10-11-2007   #10 (permalink)
Kirk Munro


 
 

Re: Newbie - Output to a text file problem.

I need to add to this. Personally, like Brandon, I like to dive right in
and take the sink or swim approach. I listened to a presentation or two and
then started experimenting. What helped me the most, though, has been
learning how to get the rich information about PowerShell itself directly
from PowerShell, through help documentation, cmdlet attributes, or
reflection. Learning how to find what you're looking for is key (as silly
as that sounds). Five cmdlets go a long way to helping in this approach:
Get-Command, Get-Help, Get-Alias, Get-PSDrive and Get-Member.

Here are some questions and their answers mostly in one-liners to help get
you started.

I've heard PowerShell cmdlets all follow a verb-noun syntax. What verbs are
available in PowerShell?
Get-Command -CommandType Cmdlet | Group-Object -Property Verb -NoElement

What cmdlets use the verb "out"?
Get-Command -Verb Out

What cmdlets use the noun "command"?
Get-Command -Noun Command

What examples are available for Tee-Object?
Get-Help Tee-Object -examples

What is the full help information for Tee-Object?
Get-Help Tee-Object -Full | more

What are the non-common parameters of Set-Location (with their
documentation)?
Get-Help Set-Location -Parameter *

What cmdlets have a parameter like "path"?
Get-Help * -Parameter *path* | Sort-Object -Property Name

I need general help on operators. Where can I find that information?
Get-Help -Category HelpFile,Provider | Where-Object { (Get-Help $_.Name |
Out-String).Contains("operator") }

Now that I see the help topics and providers that reference the keyword
"operator", how do I see the details?
Get-Help about_operator
Get-Help Variable
# etc.

What aliases are defined for the ForEach-Object cmdlet?
Get-Alias | Where-Object {$_.Definition -eq "ForEach-Object"}

I've heard PowerShell even treats the registry as a drive. What other
drives are there?
Get-PSDrive

How do I start browsing the Registry in PowerShell?
Set-Location HKLM: # or cd HKLM:
Get-ChildItem # or dir
Set-Location Software # or cd Software
# etc.

How do I add all of my installed PowerShell snapins to the current session
so that I can use their cmdlets too?
Get-PSSnapin -Registered | Add-PSSnapin

I need to enumerate a bunch of services and then do other things. Since
these are enumerated as objects, how can I tell what I can do with them
(i.e. how can I tell what properties and methods they have)?
Get-Service | Get-Member

That should be enough to help someone get started in the right direction.

-
Kirk Munro
Poshoholic
http://poshoholic.com

"Brandon Shell [MVP]" <a_bshell.mask@xxxxxx> wrote in message
news:29d4f64619d08c9da3d1cd86f98@xxxxxx
Quote:

>I believe the best way to learn something is to use it and while that may
>sound obvious it absolutely true.
>
> That said everyone had different learning styles... mine is trial by fire
>
> My first VBScript was close to 1200 lines and my first Powershell Script
> was close to 720.
> My point is that for me, I just went all out full board and picked that
> seemed to be an impossible task for someone that knew next to nothing
> about .NET and even less about Powershell. I think developer have a
> headstart when it comes to Powershell. It is an Admin shell, and there is
> no dout about that, but it was built around a VERY dev centric product
> .NET.
>
> I think the NG, Blogs, and Books are a great start.
>
> Brandon Shell
> ---------------
> Blog: http://www.bsonposh.com/
> PSH Scripts Project: www.codeplex.com/psobject
>
> K> Hi Brandon,
> K> K> Thanks for the comprehensive reply.
> K> K> I've tried the out-file - append method and it work just great,
> K> haven't tried the others yet but I'm sure they will work too.
> K> K> Brandon, in the last week or two since I fell in love with
> PowerShell
> K> I've began reading "PowerShell in Action" and started
> K> actually writing some scripts and reading everything I can about it.
> K> What I would like to ask you is "how does one best become
> K> proficient in PowerShell?" How did you learn it? If you see what I
> K> mean.
> K> Keep up the good work!
> K> K> Thanks,
> K> Stuart
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
How to display screen output to text file in PowerShell PowerShell
Output all content of text file in different columns PowerShell
output .ps1 script to text file PowerShell
Newbie - Writing to host and a text file PowerShell
Reccording DOS output to a text file Vista security


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46