Basically I have this code:
$file = $web.GetFile("Pages/default.aspx")
$file.CheckOut()
and I was wondering if there is anyway to use a pipe and the powershell equivalent of this to rewrite it as:
$web.GetFile("Pages/default.aspx") | $this.CheckOut()
When I try this I get the error:
Expressions are only allowed as the first element of a pipeline.
I also tried using $_ instead of $this but got the same error.
What you’re looking for is
$_and it represents the current object in the pipeline. However you can only access$_in a scriptblock of a command that takes pipeline input e.g.:However there are aliases for the Foreach-Object cmdlet {Foreach and %} and -Process is the default parameter so this can be simplified to:
One other point, the GetFile call appears to return a single file so in this case, the following would be the easiest way to go:
Of course, at this point you no longer have a variable containing the file object.