Function
Convert-Size {
<#
.SYSNOPSIS
Converts
a size in bytes to its upper most value.
.DESCRIPTION
Converts
a size in bytes to its upper most value.
.PARAMETER
Size
The size
in bytes to convert
.NOTES
Author:
Boe Prox
Date
Created: 22AUG2012
.EXAMPLE
Convert-Size
-Size 568956
555 KB
Description
-----------
Converts the
byte value 568956 to upper most value of 555 KB
.EXAMPLE
Get-ChildItem | ? {!
$_.PSIsContainer} | Select -First 5 | Select Name, @{L='Size';E={$_ |
Convert-Size}}
Name
Size
----
----
Data1.cap
14.4 MB
Data2.cap
12.5 MB
Image.iso
5.72 GB
Index.txt
23.9 KB
SomeSite.lnk
1.52 KB
SomeFile.ini
152 bytes
Description
-----------
Used with
Get-ChildItem and custom formatting with Select-Object to list the uppermost
size.
#>
[cmdletbinding()]
Param (
[parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[Alias("Length")]
[int64]$Size
)
Begin {
If (-Not $ConvertSize) {
Write-Verbose ("Creating
signature from Win32API")
$Signature
= @"
[DllImport("Shlwapi.dll", CharSet = CharSet.Auto)]
public
static extern long StrFormatByteSize( long fileSize, System.Text.StringBuilder
buffer, int bufferSize );
"@
$Global:ConvertSize = Add-Type -Name
SizeConverter -MemberDefinition $Signature -PassThru
}
Write-Verbose ("Building
buffer for string")
$stringBuilder
= New-Object
Text.StringBuilder 1024
}
Process {
Write-Verbose ("Converting
{0} to upper most size" -f $Size)
$ConvertSize::StrFormatByteSize( $Size, $stringBuilder, $stringBuilder.Capacity ) | Out-Null
$stringBuilder.ToString()
}
}
function
Find-File([string]$folderPath)
{
if (Test-Path $folderPath)
{
try
{
[string[]]$childFolderPaths = [System.IO.Directory]::GetDirectories($folderPath)
foreach($childPath in $childFolderPaths)
{
[System.IO.DirectoryInfo]$di = New-Object System.IO.DirectoryInfo($childPath)
if (($di.Attributes -band [System.IO.FileAttributes]::ReparsePoint) -ne [System.IO.FileAttributes]::ReparsePoint)
{
<#
This is the recursive part. That is, this function calls itself in order
to process child directories.
#>
Find-File $childPath
}
}
[string[]]$filePaths = [System.IO.Directory]::GetFiles($folderPath, "*.*", [System.IO.SearchOption]::TopDirectoryOnly)
foreach($filePath in $filePaths)
{
<#
Here
is where you'd take whatever action you want. I'm looking for files named
AssemblyInfo.cs and displaying their full paths.
#>
if (([System.IO.Path]::GetFileName($filePath)) -like "*.ost")
{
#Write-Host $filePath -ForegroundColor DarkGreen
Return $filePath
}
}
}
catch
{Write-Host ($Error[0]) -ForegroundColor Red}
}
}
#5297
cls
$AllUsersPath = "C:\Users"
$AllUsersPath = get-ChildItem $AllUsersPath
-Name
for($counter = 0; $counter -lt $AllUsersPath.Count; $counter++)
{
#$AllUsersPath[$counter]
$OST_Folder
= "C:\users\"
$OST_Folder
+= $AllUsersPath[$counter]
$OST_Folder
+= "\AppData\Local\Microsoft\Outlook"
#$OST_Folder
if ((Test-Path -Path $OST_Folder) -eq $true)
{
if ((get-ChildItem $OST_Folder | where { $_.Extension -eq ".ost"}).Exists -eq $true)
{
$result
= Find-File
$OST_Folder
$result_size
=
Convert-Size -Size ((Get-ItemProperty $result).Length)
Write-Host "Do you want delete?"
Write-Host "file name: " -ForegroundColor
White -NoNewline
Write-Host $result
-ForegroundColor
Green
Write-Host "file size: " -NoNewline -ForegroundColor
White
Write-Host $result_size
-ForegroundColor
Green
Write-Host "-------------------"
$delete_request
= Read-Host " Yes; any key - not"
switch ($delete_request)
{
'y' {
cls
remove-Item $result; Write-Host "OST file "
Write-Host "file name: " -ForegroundColor
White -NoNewline
Write-Host $result
Write-Host "file size: " -ForegroundColor
White -NoNewline
Write-Host $result_size
Write-Host " DELETED" -ForegroundColor
Green}
default {
cls
Write-Host "OST file "
Write-Host "file name: " -ForegroundColor White -NoNewline
Write-Host $result
Write-Host "file size: " -ForegroundColor White -NoNewline
Write-Host $result_size
Write-Host " NOT DELETED" -ForegroundColor Red
}
}
}
}
}
Write-Host "-------------------"
Write-Host "script FINISHED" -ForegroundColor Green
Same can have free space profit by delete Search database (can have size 50 GB)
c:\>sc config wsearch
start= disabled
c:\>sc stop wsearch
c:\>del C:\ProgramData\Microsoft\Search\Data\Applications\Windows\Windows.edb
and disable Hybernate mode (profit – 2..8 GB)
c:\>powercfg.exe /hibernate off
don’t forget check and clean if need “C:\Windows\ccmcache”
and windows update folder same
P.S. Below start powershell script from command line and auto
request UAC window for start as administrator. Script must be located on
same folder where PS script: Start-Delete_OST.bat
::::::::::::::::::::::::::::::::::::::::::::
:: Automatically
check & get admin rights V2
::::::::::::::::::::::::::::::::::::::::::::
@echo off
CLS
ECHO.
ECHO =============================
ECHO Running Admin
shell
ECHO =============================
:init
setlocal DisableDelayedExpansion
set "batchPath=%~0"
for %%k in (%0) do set batchName=%%~nk
set "vbsGetPrivileges=%temp%\OEgetPriv_%batchName%.vbs"
setlocal EnableDelayedExpansion
:checkPrivileges
NET FILE 1>NUL 2>NUL
if '%errorlevel%' == '0' ( goto
gotPrivileges ) else ( goto getPrivileges )
:getPrivileges
if '%1'=='ELEV' (echo ELEV & shift /1 & goto
gotPrivileges)
ECHO.
ECHO **************************************
ECHO Invoking UAC for Privilege
Escalation
ECHO **************************************
ECHO Set UAC = CreateObject^("Shell.Application"^) > "%vbsGetPrivileges%"
ECHO args = "ELEV " >> "%vbsGetPrivileges%"
ECHO For Each strArg in WScript.Arguments >> "%vbsGetPrivileges%"
ECHO args = args ^& strArg ^& " " >> "%vbsGetPrivileges%"
ECHO Next >> "%vbsGetPrivileges%"
ECHO UAC.ShellExecute "!batchPath!", args, "", "runas", 1 >> "%vbsGetPrivileges%"
"%SystemRoot%\System32\WScript.exe" "%vbsGetPrivileges%" %*
exit /B
:gotPrivileges
setlocal & pushd .
cd /d %~dp0
if '%1'=='ELEV' (del "%vbsGetPrivileges%" 1>nul 2>nul & shift /1)
::::::::::::::::::::::::::::
::START
::::::::::::::::::::::::::::
REM Run shell as admin (example) - put here code
as you like
ECHO %batchName% Arguments: %1 %2 %3 %4 %5 %6 %7 %8 %9 %~dp0
reg add "HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /v ExecutionPolicy /d RemoteSigned /f
powershell.exe %~dp0\OST_Delete.ps1
pause
reg add "HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell" /v ExecutionPolicy /d Restricted /f
No comments:
Post a Comment