locked
help on listing the files. RRS feed

  • Question

  • Hi All,

    Iam new to the powershell learned basic commands only for 3 days (knew it takes days to learn until handon experience),Now Iam testing in my server to identify the files which has extensions .bak,.trn which of older than 15 days but condition is

    a)I have 100 of servers so i need to use the servers.txt then copy the servers in it.

    b)I have to have the output format like server name,path of the file,size(interms of GB,MB),modified date/time but this outcome has to look out all the Drives of the folders then put in the excel sheet.

    can some one help me on this,I prepared below one but stuck

    $numberOfDays = 15
    $Now = Get-Date
    $TargetFolder = Get-PSDrive | select C  #just for test added C
    $LastWrite = $Now.AddDays(-$numberOfDays)
    $Files = get-childitem $TargetFolder -include *.BAK*,*.bak*,*.trn* -File -Recurse -ErrorAction SilentlyContinue | Where {$_.LastWriteTime -le “$LastWrite”} 
    
    foreach ($File in $Files)
    {
        write-host “listing $File” -foregroundcolor “Green”;
        get-childitem $File
    }
    #But I havent included the export.csv where it required because iam not getting out put with the above script  
    
    
    Kindly assist and appreciated your help!

    Regards, S_NO "_"

    Thursday, December 13, 2018 4:48 PM

All replies

  • All of your questions will be answered by learning basic PowerShell instead of guessing.

    Do the tutorial until you understand the basics. All of your questions will be answered.  THe links willalso help you understand how to format your code correctly.

    Get-Childitem $TargetFolder -include *.bak, *.trn -File -Recurse |
        Where-Object { $_.LastWriteTime -le $LastWrite } |
        Export-Csv output.csv
    
    You cannot learn PowerShell by blindly copying code you see on the Internet.


    \_(ツ)_/


    • Edited by jrv Thursday, December 13, 2018 6:41 PM
    Thursday, December 13, 2018 6:41 PM
  • All of your questions will be answered by learning basic PowerShell instead of guessing.

    Do the tutorial until you understand the basics. All of your questions will be answered.  THe links willalso help you understand how to format your code correctly.

    Get-Childitem $TargetFolder -include *.bak, *.trn -File -Recurse |
        Where-Object { $_.LastWriteTime -le $LastWrite } |
        Export-Csv output.csv
    You cannot learn PowerShell by blindly copying code you see on the Internet.


    \_(ツ)_/


    I understand that, I said Iam still learning & basics too.. not guessing for the question which I posted..just need an help how I can achieve thats iit..

    Regards, S_NO "_"

    Thursday, December 13, 2018 9:21 PM

  • I understand that, I said Iam still learning & basics too.. not guessing for the question which I posted..just need an help how I can achieve thats iit..

    Regards, S_NO "_"

    I posted the answer t your question as best as I can understand what you are trying to ask.  The code posted shows you have not even learned any basic concepts.  Yu have not tried to take any course, video or book tutorial and are just guessing based on what you see.  You cannot learn PS by guessing.  Take the posted tutorial on MVA and do it carefully one section at a time.  Repeat the section until you understand the lesson before moving to next section. In the end you will understand the basics of PowerShell.

    If you only want to use PowerShell like a simple batch file then do at least the first two lessons. That can be don in a short afternoon.  The tutorial is given by the lead engineer for the PowerShell team.

    Do at least the following sections carefully:

    1. Don't fear the shell
    2. The help system
    3. The pipeline: getting connected & extending the shell
    4. Objects for the Admin


    \_(ツ)_/

    Thursday, December 13, 2018 9:58 PM

  • I understand that, I said Iam still learning & basics too.. not guessing for the question which I posted..just need an help how I can achieve thats iit..

    Regards, S_NO "_"

    I posted the answer t your question as best as I can understand what you are trying to ask.  The code posted shows you have not even learned any basic concepts.  Yu have not tried to take any course, video or book tutorial and are just guessing based on what you see.  You cannot learn PS by guessing.  Take the posted tutorial on MVA and do it carefully one section at a time.  Repeat the section until you understand the lesson before moving to next section. In the end you will understand the basics of PowerShell.

    If you only want to use PowerShell like a simple batch file then do at least the first two lessons. That can be don in a short afternoon.  The tutorial is given by the lead engineer for the PowerShell team.

    Do at least the following sections carefully:

    1. Don't fear the shell
    2. The help system
    3. The pipeline: getting connected & extending the shell
    4. Objects for the Admin


    \_(ツ)_/

    Thanks for your suggestions,Yes I will do the same & learning the same,No more round robin please,

    Regards, S_NO "_"

    Friday, December 14, 2018 4:34 AM
  • Hi,

    Thanks for your question.

    1. create the servers.txt.

    $servers=Get-Content -Path C:\servers.txt
    foreach($server in $servers)
    {
      Invoke-Command -ComputerName $server -ScriptBlock {}
    }


    2. If you want to look out all the drives, you can use the filesystem psprovider.

    Get-PSDrive -PSProvider FileSystem  | 

    %{ $obj+= Get-ChildItem $_.Root -file -Recurse -force -ErrorAction SilentlyContinue -include *.bak,*.trn | Where-Object { $_.LastWriteTime -le $LastWrite } | Select-Object -Property @{name="servername";expression="$server"},FullName,Length }

    3. Use export-CSV cmdlet export the CSV file.

    $obj | export-csv $csvpath

    You need to change my script to work your issue.

    Best Regards,

    Lee


    Just do it.

    Friday, December 14, 2018 6:27 AM
  • Hi,

    Thanks for your question.

    1. create the servers.txt.

    $servers=Get-Content -Path C:\servers.txt
    foreach($server in $servers)
    {
      Invoke-Command -ComputerName $server -ScriptBlock {}
    }


    2. If you want to look out all the drives, you can use the filesystem psprovider.

    Get-PSDrive -PSProvider FileSystem  | 

    %{ $obj+= Get-ChildItem $_.Root -file -Recurse -force -ErrorAction SilentlyContinue -include *.bak,*.trn | Where-Object { $_.LastWriteTime -le $LastWrite } | Select-Object -Property @{name="servername";expression="$server"},FullName,Length }

    3. Use export-CSV cmdlet export the CSV file.

    $obj | export-csv $csvpath

    You need to change my script to work your issue.

    Best Regards,

    Lee


    Just do it.

    Thanks LeeSeenLi- I will check this and get back to you, appreciated!

    Regards, S_NO "_"

    Friday, December 14, 2018 1:23 PM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, December 17, 2018 5:55 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee


    Just do it.

    Monday, December 24, 2018 3:12 AM