locked
Help with powershell RRS feed

  • Question

  • Hi

    I what I want to do is that it reads out all the folders from F: \ Test folder and writes it to C: \ temp without which it removes all folders containing al or AL. In the view itself in power shell ISE then shows the right but when it writes so take it with them al and AL

    Function GetFileNames([string]$path, [string]$outputFile) {
      $list = Get-ChildItem F:\test
        $list -Replace("al","")
        $list -Replace("AL","")
      $list |  Select-Object Name | Out-File $outputFile
    }
    GetFileNames ".\" "C:\temp\test.txt"
    Output to file is this:

    AL23123
    alo2323

    When it really should have this:
    23123
    2323


    Thursday, June 30, 2016 3:41 PM

Answers

  • Get-ChildItem 'D:\Test' -Directory |
            Select-Object @{Label = 'Name'; Expression = {$_.name -Replace 'abc',''}} |
            Out-File D:\File.txt

    • Marked as answer by basha91 Thursday, June 30, 2016 4:51 PM
    Thursday, June 30, 2016 4:26 PM

All replies

  • Hi,

    Using -Replace won't update your original variable. See this for an example:

    $list = 'Something'
    
    $list -replace ('Some','')
    
    $list


    Thursday, June 30, 2016 3:50 PM
  • $list -replace ('Some','')
    thing

    ??? how can i fix it?

    Thursday, June 30, 2016 4:22 PM
  • Fix what?

    Thursday, June 30, 2016 4:25 PM
  • Get-ChildItem 'D:\Test' -Directory |
            Select-Object @{Label = 'Name'; Expression = {$_.name -Replace 'abc',''}} |
            Out-File D:\File.txt

    • Marked as answer by basha91 Thursday, June 30, 2016 4:51 PM
    Thursday, June 30, 2016 4:26 PM

  • $s = 'Something'
    $s = $s -replace 'Some',''
    # $s now contains 'thing'
    

    The second line of the above uses the -replace operator to create a new string, and then it reassigns the replacement string back to $s.


    -- Bill Stewart [Bill_Stewart]

    Thursday, June 30, 2016 4:27 PM