Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Struggling to change property at end of pipeline

24 views
Skip to first unread message

ssg31415926

unread,
Apr 17, 2009, 8:27:06 AM4/17/09
to
I'm always struggling with pipelines - in particular to do something
to the object(s) I've got at the end of the pipeline. This is a
typical example:

gci -recurse | where {$_.IsReadOnly -eq $TRUE} | $_.set_IsReadOnly
($FALSE)

I want to get every file which is marked as read only and clear the
flag. The first bit works on it's own:

gci -recurse

The second bit works:

gci -recurse | where {$_.IsReadOnly -eq $TRUE}

but as soon as I try to effect a change with the third bit:

gci | where {$_.IsReadOnly -eq $TRUE} | $_.set_IsReadOnly($FALSE)

I get an error like this:

Expressions are only permitted as the first element of a pipeline.
At line:1 char:65
+ gci | where {$_.IsReadOnly -eq $TRUE} | $_.set_IsReadOnly($FALSE)
<<<<

Is it just a syntax problem or am I misunderstanding pipelines
altogether: is it not possible to change objects at the end of a
pipeline?

Uwe Kausch

unread,
Apr 17, 2009, 12:24:41 PM4/17/09
to
Hi ssg31415926,

ssg31415926 wrote:
> I'm always struggling with pipelines - in particular to do something
> to the object(s) I've got at the end of the pipeline. This is a
> typical example:
>
> gci -recurse | where {$_.IsReadOnly -eq $TRUE} | $_.set_IsReadOnly
> ($FALSE)
>
> I want to get every file which is marked as read only and clear the
> flag. The first bit works on it's own:
>
> gci -recurse
>
> The second bit works:
>
> gci -recurse | where {$_.IsReadOnly -eq $TRUE}
>
> but as soon as I try to effect a change with the third bit:
>
> gci | where {$_.IsReadOnly -eq $TRUE} | $_.set_IsReadOnly($FALSE)

Try this, I got it running on this way ...
$ReadOnlyFiles = gci -recurse | where {$_.IsReadOnly -eq $TRUE}
foreach ($file in $ReadOnlyFiles) {$file.set_IsReadOnly($FALSE)}

>
> I get an error like this:
>
> Expressions are only permitted as the first element of a pipeline.
> At line:1 char:65
> + gci | where {$_.IsReadOnly -eq $TRUE} | $_.set_IsReadOnly($FALSE)
> <<<<
>
> Is it just a syntax problem or am I misunderstanding pipelines
> altogether: is it not possible to change objects at the end of a
> pipeline?

I think you can use "$_" only one time, otherwise you have to use variables and loops like I did it. For further information about pipes this page "http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/pipe.mspx" is helpful.

Regards from Munich,
Uwe

Uwe Kausch

unread,
Apr 17, 2009, 12:36:42 PM4/17/09
to
Hi *,

ssg31415926 wrote:
> I'm always struggling with pipelines - in particular to do something
> to the object(s) I've got at the end of the pipeline. This is a
> typical example:
>
> gci -recurse | where {$_.IsReadOnly -eq $TRUE} | $_.set_IsReadOnly
> ($FALSE)
>
> I want to get every file which is marked as read only and clear the
> flag. The first bit works on it's own:
>
> gci -recurse
>
> The second bit works:
>
> gci -recurse | where {$_.IsReadOnly -eq $TRUE}
>
> but as soon as I try to effect a change with the third bit:
>
> gci | where {$_.IsReadOnly -eq $TRUE} | $_.set_IsReadOnly($FALSE)

Just try this please ...
gci -recurse | where {$_.IsReadOnly -eq $TRUE} | Foreach-Object {$_.set_IsReadOnly($FALSE)}

>
> I get an error like this:
>
> Expressions are only permitted as the first element of a pipeline.
> At line:1 char:65
> + gci | where {$_.IsReadOnly -eq $TRUE} | $_.set_IsReadOnly($FALSE)
> <<<<
>
> Is it just a syntax problem or am I misunderstanding pipelines
> altogether: is it not possible to change objects at the end of a
> pipeline?

For further information about pipes the page "http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/pipe.mspx" is helpful.

Regards from Munich,
Uwe

ssg31415926

unread,
Apr 18, 2009, 3:56:50 AM4/18/09
to
Ah, thanks. Your suggestion reminded me about using foreach-object in
the pipeline. I wanted a one-liner - I feel more like a 'proper'
PowerSheller when I can do one :-)

I managed to get this to work:

gci -recurse -force | where { $_.IsReadOnly -eq $TRUE } | foreach
{ $_.set_IsReadOnly($FALSE) }

Cheers

> For further information about pipes the page "http://www.microsoft.com/technet/scriptcenter/topics/winpsh/manual/pi..." is helpful.
>
> Regards from Munich,
> Uwe

0 new messages