locked
How would I rewrite this shell script in PowerShell? RRS feed

  • Question

  • This shell script lists the size of a directory and its subdirectories alphabetically then lists the name of the subdirectory with the highest disk usage along with its size. I'm completely new to PowerShell and would like to know how I would rewrite this script in PowerShell. Thanks in advance.

    #!/bin/sh
    
    myvar="$PWD" 
    
    declare -i x=1
    while true; do
      $clear
      echo  "Enter directory path or 1 for current directory ";
      read INPUT ;
    
      case "$INPUT" in
        1)INPUT="$myvar" 
      esac
    
    
    echo  "Disk space use (in KB) for each subdirectory  ";
    if [ -d "$INPUT"  ]
      then 
     echo "Directory exists ";
    else
     echo "Directory does not exist ";
     fi
    
    for d in "$INPUT"/*; do    
    
    if [ -d "$d"  ]
    then  
    du -h "$d"
     x=x+1; 
      fi
    done
    
    echo  "The subdirectory with the maximum disk space use:";
    
     
    du -m "$INPUT"| sort -nr | head -n 2
    
    done
    


    Tuesday, April 15, 2014 8:16 PM

Answers

  • PowerShell uses looping constructs like any other scripting language. You'll need to at least make an attempt at translating this yourself because this forum isn't a free code-writing service. If you get stuck, you can ask for help.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Friday, June 6, 2014 9:16 PM
    Tuesday, April 15, 2014 8:26 PM

All replies

  • PowerShell uses looping constructs like any other scripting language. You'll need to at least make an attempt at translating this yourself because this forum isn't a free code-writing service. If you get stuck, you can ask for help.

    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Friday, June 6, 2014 9:16 PM
    Tuesday, April 15, 2014 8:26 PM
  • Yes.  It is easy.  Start here: : http://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx

    ¯\_(ツ)_/¯

    Tuesday, April 15, 2014 8:31 PM
  • The PowerShell version of this is only xxxx four lines.


    ¯\_(ツ)_/¯


    • Edited by jrv Tuesday, April 15, 2014 8:33 PM
    Tuesday, April 15, 2014 8:32 PM