Dead_Eyes
asked on
powershell ForEach-Object help
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
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
I believe the $_.readonly is a refference to an attribute of whatever the object is you are doing the Foreach on.
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
ForEach-Object -process { $_.
view below for $.
http://stackoverflow.com/questions/5791937/whats-the-meaning-of-double-underscore-value
$_ is a variable or placeholder
http://www.computerperformance.co.uk/powershell/powershell_dollar_variable.htm
http://www.computerperformance.co.uk/powershell/powershell_dollar_variable.htm
ASKER
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)
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)
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.
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.
ASKER
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)
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)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.