You can actually do this directly in msbuild so that it works from VS and most CI systems without any extra steps (e.g. PowerShell scripts), even on the cross-platform dotnet CLI and mono.
You can create a file named Directory.Build.targets in the project or solution (> affects all projects) directory with the following contents (assuming MSBuild 15 / VS 2017. if lower, you'll need manual imports or insert this target into every project)
<Project>
<Target Name="DetermineOctopackVersion" BeforeTargets="Build" Condition=" '$(RunOctoPack)' == 'true' ">
<Exec Command="git rev-parse HEAD" ConsoleToMSBuild="true" StandardOutputImportance="low">
<Output TaskParameter="ConsoleOutput" PropertyName="GitCommitHash" />
</Exec>
<PropertyGroup>
<VersionDatePart>$([System.DateTime]::get_Now().ToString(yyyyMMdd.HHmmss))</VersionDatePart>
<OctopackPackageVersion>2.$(VersionDatePart)-git-$(GitCommitHash.Substring(0,16))</OctopackPackageVersion>
</PropertyGroup>
</Target>
</Project>
If you want the property to be set on every build, not only when RunOctoPack is true, simply remove the Condition attribute.
But if you need to do it in PowerShell, you can call the git command and capture its output:
$commitHash = (git rev-parse HEAD).Substring(0,16)
$versionDatePart = [System.DateTime]::Now.ToString('yyyyMMdd.HHmmss')
$version = "1.$versionDatePart-git-$commitHash"
If you really want to avoid calling into git (though no decent build system setup is complete without a git installation..) you can also use [System.IO.File]::ReadAllText(".git\refs\heads\master") both in PS or MSBuild to read from the git directory (on detached state, .git\HEAD will contain the hash, when a branch is used it will contain the location of the file to look for the hash).