Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

powershell ForEach-Object help

Posted on 2012-04-09
7
Medium Priority
?
724 Views
Last Modified: 2012-04-09
Hi I am very new to powershell and prob a very simple question but I have the following line in a script:
ForEach-Object -process { $_.isreadonly = $True }

How can I see what other process's I can use apart from $_.isreadonly?
and what is the $_. about?

I do understand I can replace "ForEach-Object -process" with "%" but am trying to keep my scripts as simple and clear as possible. Thanks in advance
0
Comment
Question by:Dead_Eyes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 35

Expert Comment

by:Joseph Daly
ID: 37824140
I believe the $_.readonly is a refference to an attribute of whatever the object is you are doing the Foreach on.
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37824156
type below in power shell and press tab to view more options what you can have for this command.

ForEach-Object -process { $_.

view below for $.
http://stackoverflow.com/questions/5791937/whats-the-meaning-of-double-underscore-value
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37824168
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:Dead_Eyes
ID: 37824188
thanks xxdcmast I had got that far though
Anuroopsundd I have tried that and although i get a few more options I do not get the isreadonly (maybe it is tied to the object I am getting information on but there should be a way to view which process's I can manipulate (usually some form of extensive help with powershell from past experience)
0
 
LVL 6

Expert Comment

by:slidingfox
ID: 37824343
What are you trying to acheive with your script? The Foreach-Object usually has something piped into it e.g.

Get-Process notepad | Foreach-Object { $_.Kill() }

The above command will list all the running instances of notepad, then foreach instance, it will call the Kill method to stop the process.

If you want to see all the options you have available to use in your ForEach scriptblock, use the Get-Member cmdlet.

Get-Process | Get-Member

This will list all the Properties and Methods available to you for each object type.

The $_ is a placeholder. Everytime the Foreach loop runs, the $_ represents a the next item that was passed in. If you ran the above script with 3 copies of notepad running, each of the notepad processes would be passed into $_ individually, then the Kill method would run. Once the method had run, $_ would represent the next process passed in.
0
 

Author Comment

by:Dead_Eyes
ID: 37824468
Thanks now I have got it. I am using the script to change the attribute on iso files. Full text below:
get-childitem -recurse -filter "*.iso" | ForEach-Object -process { $_.isreadonly = $True }

How would I use the get member in this instance to see what my options (possible -process) are? (thanks for bearing with)
0
 
LVL 6

Accepted Solution

by:
slidingfox earned 400 total points
ID: 37824727
get-childitem -recurse -filter "*.iso" | Get-Member

This will show you a list of all the properties and methods for the object.

If you want to see the values for all the properties for a particular item, you could us

Get-Childitem <filename>.iso | Format-List *

If you run that on a directory though, the output will be difficult to read, as it'll bring back all the values, for all the properties for every item.
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question