Read from folder in Powershell

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Trevor Freedland Trevor Freedland 2 months, 3 weeks ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #45380
    Profile photo of
    Anonymous

    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"
    

    In the output file i get this:
    AL23123
    alo2323
     
    When it really should get this:
    23123
    2323

    #45394
    Profile photo of Aaron Hardy
    Aaron Hardy
    Participant

    You need to use the Replace() method on $list variable rather than assigning a parameter to it (-Replace). And because PowerShell isn't case-sensitive, you only need to use it once:

    Function GetFileNames([string]$path, [string]$outputFile) {
      $list = Get-ChildItem F:\test
        $list = $list.Replace("al","") # Will replace any variation of 'al' - 'aL', 'AL', etc.
      $list |  Select-Object Name | Out-File $outputFile
    }
    GetFileNames ".\" "C:\temp\test.txt"
    #45396
    Profile photo of
    Anonymous

    ok its thanks but its posible to

    $dataSource = “Server\SQL”
    $user = “Admin”
    $pwd = “Test”
    $database = “Tester”
    $connectionString = “Server=$dataSource;uid=$user; pwd=$pwd;Database=$database;Integrated Security=False;”
    
    $query = “Select ID from Costomer”
    
    $connection = New-Object System.Data.SqlClient.SqlConnection
    $connection.ConnectionString = $connectionString
    $connection.Open()
    $command = $connection.CreateCommand()
    $command.CommandText = $query
    
    $result = $command.ExecuteReader()
    
    $table = new-object “System.Data.DataTable”
    $table.Load($result)
    
    $table | Format-Table ID -Autosize| Out-File C:\Users\xxxx\Desktop\Db.txt
    $connection.Close()

    Its that posible to remove ID on top of file

    #45743
    Profile photo of
    Anonymous

    Its write with AL

    #45994
    Profile photo of Trevor Freedland
    Trevor Freedland
    Participant

    Hi Vijitharan, You can remove the 'ID' heading on the top of file output by using the -HideTableHeaders SwitchParameter. E.g:

    $table | Format-Table ID -Autosize -HideTableHeaders | Out-File C:\Users\xxxx\Desktop\Db.txt
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.