Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6983713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:27:18+00:00 2026-05-27T18:27:18+00:00

How can I save the results from a Powershell command to a MS SQL

  • 0

How can I save the results from a Powershell command to a MS SQL DB?

For example:

I run this at a Powershell prompt: get-psdrive and it returns some results in a column view.

How can I take each element of the result and log it into a separate DB row/column?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:27:19+00:00Added an answer on May 27, 2026 at 6:27 pm

    I recommend saving the results of your command to a variable. Such as:

    $drives = Get-PSDrive
    

    The variable can be indexed like this:

    First Element:

    $drives[0]
    

    Last Element:

    $drives[-1]
    

    You can iterate through each element with foreach:

    foreach ($drive in $drives) {
        # current drive is $drive
    }
    

    Or the ForEach-Object cmdlet:

    $drives | ForEach-Object {
        # current drive is $_
    }
    

    Now that you have the data to populate your table with you are ready to connect to the database and perform the database record inserts.

    You can make use of the Powershell SQL server cmdlets or you can connect using .NET objects. Depending on what version of SQL server you have will drive your choice on which to use. SQL Server 2008 has Powershell cmdlets, 2005 does not. There is a wealth of information about the SQL server 2008 Powershell integration here. For SQL Server 2005 you have some different options. This question answer here provides a list of Powershell options to use with SQL Server 2005.

    More Info:

    When Powershell displays object information it uses a type system to selectively determine what properties of the object to display on the screen. Not all of the object’s are displayed. Powershell uses XML files to determine what properties to display which are stored in the Powershell directory:

    dir $PSHOME/*format* | select Name
    

    The objects returned from Get-PsDrive are of type System.Management.Automation.PSDriveInfo. The file PowerShellCore.format.ps1xml tells the formatting engine what properties to display in the Powershell window. It just might be that these are the exact properties you are looking for however many objects have additional properties that are not displayed. For example an object of type System.IO.DirectoryInfo will not have all it’s properties displayed by default. You can view the rest of the objects properties using the Get-Member cmdlet, for example:

    Get-Item $env:windir | Get-Member
    

    This will show all of the object’s methods and properties. You can also view all of the object’s properties using the Select-Object cmdlet using a wildcard for the property parameter:

    Get-Item $env:windir | Select-Object -Property *
    

    To access an objects properties values use the following syntax:

    $objectVariable.ObjectProperty
    

    Now that you know how to view an objects properties and access their values you’ll need to use this to construct an Insert SQL statement. Here is an example using the Invoke-SqlCmd cmdlet provided with SQL Server 2008.

    Invoke-Sqlcmd -ServerInstance $env:COMPUTERNAME -Database Test -Query "Insert MyTable values ('a', 'b')"
    

    Here’s an example looping through objects returned from Get-PsDrive assuming you have a table called MyTable and it has at least two columns which accept textual data:

    Get-PsDrive | ForEach-Object {
        $providerName = $_.Name
        $providerRoot = $_.Root
        Invoke-Sqlcmd -ServerInstance $env:COMPUTERNAME -Database Test -Query "Insert MyTable values ('$providerName', '$providerRoot')"
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For Yii framework, can we set attribute for save function using object results from
You can save a SQL Server 2000 DTS package as a VB .BAS file.
How can I run a ruby piece of code from a buffer, without actually
I'm trying to parse results from queries over HTTP that can return up to
So I'm trying to get the results from a stored proc (200k rows+) into
I want to save image from a url in assets folder. I can save
I have a database (MySql) and need to store some results from a web
I can save image to iPhone (iPhone Photo Albums \ Saved Photos) with UIImageWriteToSavedPhotosAlbum(image,
Internet Explorer can save its content as an MHTML file using the Save As...
Actually I can save it in swf and in avi formats. Is there any

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.