bin/GetOneDriveDirectory.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#Created in following environment
#Name Value
#---- -----
#PSVersion 5.1.14393.1358
#PSEdition Desktop
#PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
#BuildVersion 10.0.14393.1358
#CLRVersion 4.0.30319.42000
#WSManStackVersion 3.0
#PSRemotingProtocolVersion 2.3
#SerializationVersion 1.1.0.1

function GetOneDriveUserFolder($account)
{    
    $OneDriveUserFolder=$account.GetValue("UserFolder")
    Write-Verbose "OneDrive drectory: '$OneDriveUserFolder' (Result)"
    return $OneDriveUserFolder
}

function Get-OneDriveDirectory()
{
    [cmdletbinding()]
    param ([switch]$Personal,[switch]$Business,[switch]$JustDirectory)

    if ($Personal.IsPresent){Write-Verbose "Looking for Personal OneDrive directory"}
    if ($Business.IsPresent){Write-Verbose "Looking for Bussienss OneDrive directory"}

    $anyOneDrive=!($Personal.IsPresent -or $Business.IsPresent)
    $lookForPersonal=$Personal.IsPresent -or $anyOneDrive
    $lookForBussiness=$Business.IsPresent -or $anyOneDrive
    
    $result=@{}

    $oneDriveAccounts=Get-ChildItem -Path "HKCU:\SOFTWARE\Microsoft\OneDrive\Accounts"
    foreach($account in $oneDriveAccounts)
    {
        Write-Verbose "One of OneDrive registry keys: $account"
        if ($account.PSChildName -eq "Personal" -and $lookForPersonal)
        {
            Write-Verbose "Found Personal Registry key"
            $result.Personal=$(GetOneDriveUserFolder $account)
            #return GetOneDriveUserFolder $account
            
        }
        if ($account.GetValue("Business") -eq 1 -and $lookForBussiness)
        {
            Write-Verbose "Found Bussines Registry key"
            $result.Business=$(GetOneDriveUserFolder $account)
            #return GetOneDriveUserFolder $account
        }
    }
    
    if ($result.Count -gt 0)
    {
        if ($JustDirectory.IsPresent)
        {
            return $result.Values
        }
        else
        {
            return $result
        }
    }
    else
    {
        throw [System.Exception] "Probably OneDrive is not installed"
    }
}


Export-ModuleMember -Function Get-OneDriveDirectory