comment command lines

Welcome Forums General PowerShell Q&A comment command lines

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #5824

    Points: 0
    Rank: Member

    by willbs at 2013-04-19 08:30:38

    i want to comment out some command lines in a group of powershell scripts but the conversion is not happening, i'm sure it has to do with my handling of the # in $newfile3, here is my code

    $Folder = 'C:\PowerShell\PS_Star*.*'
    $file3 = 'Invoke-Item $global:OutputReportFile'
    $newfile3 = '#Invoke-Item $global:OutputReportFile'

    Get-ChildItem $Folder | Where {$_ -IS [IO.FileInfo]} |
    % {

    (Get-Content $_.FullName) -replace $file3,$newfile3 | Set-Content $_.FullName

    Write-Host "Processed: " + $_.FullName


    any ideas?

    by sstranger at 2013-04-19 13:04:00

    You have placed the # between the single quotes. PowerShell takes everything between single quotes literrally.

    Stefan Stranger [Microsoft]

    by willbs at 2013-04-19 15:19:15

    that's what i thought too but the code doesn't perform the remove and replace

    by ArtB0514 at 2013-04-22 06:12:52

    The use of the single quote marks in your replacement text looks correct to me. Your problem is elsewhere.

    To start debugging, I would just process the first file found and break up the pipeline to see which step fails. NOTE: this scriptlet assumes PowerShell V3. (Replace "-File" with "| Where-Object {-not $_.PSIsContainter}" for V2)

    Get-ChildItem $Folder -File | Select-Object -First 1 | Foreach-Object {
    $NewContent = (Get-Content $_.FullName) -replace $file3,$newfile3
    $NewContent | Set-Content "$($_.DirectoryName)\$($_.BaseName).TST"
    Write-Host "Processed: " + $_.FullName}

    This will leave you with a variable ($NewContent) that you can examine, plus the original file and the new file with the extension changed to ".TST" that you can compare. I'd check to see if the -Replace is actually working against the whole file, or if you might need to run it against each line separately.

The topic ‘comment command lines’ is closed to new replies.