Powershell: Read-Host not working when ran from jump drive

I am writing a script that will get a data count off of HDDs that we work on.  Everything is working and when I added a Read-Host so that you could enter the drive letter of the connected HDD it worked until I put it on a jump drive and now all it reads is the jump drive drive letter.

RunMe.bat

@echo off
::  by: Joseph Monarch
pushd %~dp0
powershell -nologo -file "datacount.ps1"
pause

datacount.ps1

# User input for drive letter
$drive = Read-Host "Enter drive letter for data count. (ex. C)"

# Lists in an array all directories and files that need to be counted
$directory = get-childitem -Path "$drive:\*" -force -exclude *windows*, *Intel*, '*Program Files*', *.ini*, *Users*, *PerfLogs*, *eula*, *.dll*, *VC*, *.sys*, *Recycle*, '*Documents and Settings*', *ProgramData*, *Recovery*, '*System Volume Information*'
$directory += get-childitem -Path "$drive:\Users\*" -exclude '*All Users*', *Default*, '*Default User*', *AppData*, '*Application Data*', *Cookies*, *.dll*, *.ini*, *Microsoft*, *NetHood*, *PrintHood*, *Recent*, *SendTo*, '*Start Menu*', *Templates*, '*Saved Games*', *Searches* 
$directory += get-childitem -Path "$drive:\Program Files\*" -force -exclude '*Common Files*', '*Internet Explorer*', *MSBuild*, '*Reference Assemblies*', *Roxio*, *Symantec*, '*Uninstall Information*', '*Windows Defender*', '*Windows Journal*', '*Windows Mail*', '*Windows Media Player*', '*Windows NT*', '*Windows Photo Viewer*', '*Windows Portable Devices*', '*Windows Sidebar*', *.ini*
$directory += get-childitem -Path "$drive:\Program Files (x86)\*" -force -exclude '*Common Files*', '*Internet Explorer*', *MSBuild*, '*Reference Assemblies*', *Roxio*, *Symantec*, '*Uninstall Information*', '*Windows Defender*', '*Windows Journal*', '*Windows Mail*', '*Windows Media Player*', '*Windows NT*', '*Windows Photo Viewer*', '*Windows Portable Devices*', '*Windows Sidebar*', *.ini* , *Adobe*, *Bonjour*, *Google*, '*InstallShield Installation Information*', *Java*, *JRE*, *Microsoft.NET*, '*Mozilla Firefox*', '*Mozilla Maintenance Service*', *MSBuild*, '*MSXML 4.0*', '*OpenOffice.org 3*', *QuickTime*, *Temp*

#Get all profile names
$profile = get-item -Path $drive:\Users\* -exclude '*All Users*', *Default*, '*Default User*'
foreach ($name in $profile) {
    $name = $name.basename
    if($name -eq "Public") {
        ""
    }
    else {
        # Search specific user folders
        if(Test-Path "$drive:\Users\$name\AppData\Local\Microsoft\Windows Mail") {
            $directory += get-item -Path "$drive:\Users\$name\AppData\Local\Microsoft\Windows Mail\*"
        }
        if(Test-Path "$drive:\Users\$name\AppData\Local\Microsoft\Outlook") {
            $directory += get-item -Path "$drive:\Users\$name\AppData\Local\Microsoft\Outlook\*"
        }
        if(Test-Path "$drive:\Users\$name\AppData\Local\Microsoft\Windows Live Mail") {
            $directory += get-item -Path "$drive:\Users\$name\AppData\Local\Microsoft\Windows Live Mail\*"
        }    
    }
}
 
# Get total size for each item in $directory in KB
$count = foreach ($dir in $directory) {
    $size = Get-ChildItem $dir -recurse -force -ErrorAction SilentlyContinue | Measure-Object -Sum Length
    $dsize = $size.sum / 1KB
    $tsize = "{0:N2}" -f $dsize
    $tsize
}

# Sum all the items in $count
function Get-Sum ($count) {
    return ($count | Measure-Object -Sum).Sum
}

$scount = Get-Sum($count)

#Display results in MB
if ($scount -le 1MB) {
    $tcount = $scount / 1KB
    $tcount = "{0:N2}" -f $tcount
    Write-Host "$tcount MB"
}
#Display results in GB
elseif ($scount -le 1GB) {
    $tcount = $scount / 1MB
    $tcount = "{0:N2}" -f $tcount
    Write-Host "$tcount GB"
}
#Display results in TB
elseif ($scount -le 1TB) {
    $tcount = $scount / 1GB

Also I get this error in regards to this line of code

Measure-Object : Property "Length" cannot be found in any object(s) input.
At C:\Users\Owner\Documents\datacount.ps1:49 char:94
+     $size = Get-ChildItem $dir -recurse -force -ErrorAction SilentlyContinue | Measure-Object <<<<  -Sum 
Length
    + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCo 
   mmand
Measure-Object : Property "Length" cannot be found in any object(s) input.
At C:\Users\Owner\Documents\datacount.ps1:49 char:94
+     $size = Get-ChildItem $dir -recurse -force -ErrorAction SilentlyContinue | Measure-Object <<<<  -Sum 
Length
    + CategoryInfo          : InvalidArgument: (:) [Measure-Object], PSArgumentException
    + FullyQualifiedErrorId : GenericMeasurePropertyNotFound,Microsoft.PowerShell.Commands.MeasureObjectCo 
   mmand

if(Test-Path "$drive:\Users\$name\AppData\Local\Microsoft\Windows Live Mail") {
            $directory += get-item -Path "$drive:\Users\$name\AppData\Local\Microsoft\Windows Live Mail\*"
        } 


July 11th, 2013 11:19am

Here is my final script:

# User input for drive letter
Write-Host "Data Count" -foregroundcolor Green
Write-Host "=========================" -foregroundcolor Green
$drive = Read-Host "Enter drive letter for data count"
Write-Host "Gathering Data Count" -foregroundcolor magenta
# Lists in an array all directories and files that need to be counted
$directory = get-childitem $drive`:\* -force -exclude *windows*, *Intel*, '*Program Files*', *.ini*, *Users*, *PerfLogs*, *eula*, *.dll*, *VC*, *.sys*, *Recycle*, '*Documents and Settings*', *ProgramData*, *Recovery*, '*System Volume Information*'
$directory += get-childitem $drive`:\Users\* -exclude '*All Users*', *Default*, '*Default User*', *AppData*, '*Application Data*', *Cookies*, *.dll*, *.ini*, *Microsoft*, *NetHood*, *PrintHood*, *Recent*, *SendTo*, '*Start Menu*', *Templates*, '*Saved Games*', *Searches* 
$directory += get-childitem $drive`:\Program` Files\* -force -exclude '*Common Files*', '*Internet Explorer*', *MSBuild*, '*Reference Assemblies*', *Roxio*, *Symantec*, '*Uninstall Information*', '*Windows Defender*', '*Windows Journal*', '*Windows Mail*', '*Windows Media Player*', '*Windows NT*', '*Windows Photo Viewer*', '*Windows Portable Devices*', '*Windows Sidebar*', *.ini*
$directory += get-childitem $drive`:\Program` Files` `(x86`)\* -force -exclude '*Common Files*', '*Internet Explorer*', *MSBuild*, '*Reference Assemblies*', *Roxio*, *Symantec*, '*Uninstall Information*', '*Windows Defender*', '*Windows Journal*', '*Windows Mail*', '*Windows Media Player*', '*Windows NT*', '*Windows Photo Viewer*', '*Windows Portable Devices*', '*Windows Sidebar*', *.ini* , *Adobe*, *Bonjour*, *Google*, '*InstallShield Installation Information*', *Java*, *JRE*, *Microsoft.NET*, '*Mozilla Firefox*', '*Mozilla Maintenance Service*', *MSBuild*, '*MSXML 4.0*', '*OpenOffice.org 3*', *QuickTime*, *Temp*

# Get all profile names
$profile = get-item -Path $drive`:\Users\* -exclude '*All Users*', *Default*, '*Default User*'
foreach ($name in $profile) {
    $name = $name.basename
    if($name -eq "Public") {
        ""
    }
    else {
        # Search specific user folders
        if(Test-Path "$drive:\Users\$name\AppData\Local\Microsoft\Windows Mail") {
            $directory += get-item $drive`:\Users\$name\AppData\Local\Microsoft\Windows` Mail\*
        }
        if(Test-Path "$dri:\Users\$name\AppData\Local\Microsoft\Outlook") {
            $directory += get-item $drive`:\Users\$name\AppData\Local\Microsoft\Outlook\*
        }
        if(Test-Path "$dri:\Users\$name\AppData\Local\Microsoft\Windows Live Mail") {
            $directory += get-item $drive`:\Users\$name\AppData\Local\Microsoft\Windows` Live` Mail\*
        }    
    }
}

# Get total size for each item in $directory in KB
$count = foreach ($dir in $directory) {
    $size = Get-ChildItem $dir -recurse -force -ErrorAction SilentlyContinue | Measure-Object -Sum Length
    $dsize = $size.sum / 1KB
    $tsize = "{0:N2}" -f $dsize
    $tsize
}

# Sum all the items in $count
function Get-Sum ($count) {
    return ($count | Measure-Object -Sum).Sum
}

$scount = Get-Sum($count)

#Display results in MB
if ($scount -le 1MB) {
    $tcount = $scount / 1KB
    $tcount = "{0:N2}" -f $tcount
    Write-Host "$tcount MB" -foregroundcolor Yellow
}
#Display results in GB
elseif ($scount -le 1GB) {
    $tcount = $scount / 1MB
    $tcount = "{0:N2}" -f $tcount
    Write-Host "$tcount GB" -foregroundcolor Yellow
}
#Display results in TB
elseif ($scount -le 1TB) {
    $tcount = $scount / 1GB
    $tcount = "{0:N2}" -f $tcount
    Write-Host "$tcount TB" -foregroundcolor Yellow
}

 I had to add a ` to the following

$directory = get-childitem $drive`:\* -force -exclude *windows*, *Intel*, ....etc

I didn't know that a : is a special character that needs to be escaped.

Free Windows Admin Tool Kit Click here and download it now
July 12th, 2013 1:29pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics