how to create new repository in github via powershell

Function New-GitHubRepository {
[cmdletbinding(SupportsShouldProcess)]

Param(
[Parameter(Position = 0, Mandatory, HelpMessage = "Enter the new repository name")]
[ValidateNotNullorEmpty()]
[string]$Name,

[string]$Description,

[switch]$Private,
[switch]$NoWiki,
[switch]$NoIssues,
[switch]$NoDownloads,
[switch]$AutoInitialize,

#license templates found at https://github.com/github/choosealicense.com/tree/gh-pages/_licenses
[ValidateSet("MIT","apache-2.0","gpl-3.0","ms-pl","unlicense")]
[string]$LicenseTemplate,

[Alias("token")]
[ValidateNotNullorEmpty()]
[string]$UserToken = $gitToken,

#write full native response to the pipeline
[switch]$Raw
)

Write-Verbose "[BEGIN  ] Starting: $($MyInvocation.Mycommand)"
#display PSBoundparameters formatted nicely for Verbose output  
[string]$pb = ($PSBoundParameters | Format-Table -AutoSize | Out-String).TrimEnd()
Write-Verbose "[BEGIN  ] PSBoundparameters: `n$($pb.split("`n").Foreach({"$("`t"*2)$_"}) | Out-String) `n" 

#create the header
$head = @{
Authorization = 'Basic ' + $UserToken
}

#create a hashtable from properties
$hash = @{
 name = $Name
 description = $Description
 private = $Private -as [boolean]
 has_wiki = (-Not $NoWiki)
 has_issues = (-Not $NoIssues)
 has_downloads = (-Not $NoDownloads)
 auto_init = $AutoInitialize -as [boolean]
}

if ($LicenseTemplate) {
    $hash.add("license_template",$LicenseTemplate)
}

$body = $hash | ConvertTo-Json

Write-Verbose "[PROCESS] Sending json"
Write-Verbose $body

#define parameter hashtable for Invoke-RestMethod
$paramHash = @{
Uri = "https://api.github.com/user/repos" 
Method = "Post"
body = $body 
ContentType = "application/json"
Headers = $head
UseBasicParsing = $True
DisableKeepAlive = $True
}

#should process
if ($PSCmdlet.ShouldProcess("$name [$description]")) {
   $r = Invoke-RestMethod @paramHash

    if ($r.id -AND $Raw) {
        Write-Verbose "[PROCESS] Raw result"
        $r

    } elseif ($r.id) {
        write-Verbose "[PROCESS] Formatted results"

        $r | Select-Object @{Name = "Name";Expression = {$_.name}},
        @{Name = "Description";Expression = {$_.description}},
        @{Name = "Private";Expression = {$_.private}},
        @{Name = "Issues";Expression = {$_.has_issues}},
        @{Name = "Wiki";Expression = {$_.has_wiki}},
        @{Name = "URL";Expression = {$_.html_url}},
        @{Name = "Clone";Expression = {$_.clone_url}}
    } else {
        
        Write-Warning "Something went wrong with this process"
    }

    if ($r.clone_url) {
      $msg = @"

To push an existing local repository to Github run these commands:
-> git remote add origin $($r.clone_url)"
-> git push -u origin master

"@
    Write-Host $msg -ForegroundColor Green

    }
}

Write-Verbose "[END    ] Ending: $($MyInvocation.Mycommand)"

}

3.75
8
Najmeh 85 points

                                    Function New-GitHubRepo{
        <#
    .SYNOPSIS
        Creates a new remote repository in GitHub
    .DESCRIPTION
        Creates a new remote repository in GitHub
    .PARAMETER UserName
        GitHub Username
    .PARAMETER ProjectName
        Name for the new remote GitHub repository
    .EXAMPLE
       New-GitHubRepo -UserName GUser -ProjectName "MyRepo"
    .NOTES
        Author: Michael Heath
          Date: 04/27/2019
    #>
Param(
    [Parameter(Mandatory = $true)][String]$UserName,
    [Parameter(Mandatory = $true)][String]$ProjectName
)

# This works for entering password
# New output file
$OutFile = ".\ex.cmd"

# Var for Quote
$q = [char]34

# Create part of the command line with the project name
$t =  "$q{\$q @@ name\$q @@ :\$q @@ $ProjectName\$q}$q"

# Remove the space and the @@ symbols
$t = $t.replace(" @@ ", "")

# Add the curl command and the project
$t = "curl -u $UserName https://api.github.com/user/repos -d " + $t

# put contents in the ex.cmd file
"@echo off" | out-file $OutFile -Encoding ascii
$t | Out-File $OutFile -Encoding ascii -Append

# Execute the ex.cmd file - you will be prompted for your password
cmd.exe /C ".\ex.cmd"

3.75 (8 Votes)
0
Are there any code examples left?
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source