如何在Windows Server 2016通过命令行获取exe的版权信息?
Great question! The wmic datafile command is limited when it comes to pulling all the details from the Properties > Details tab, but we've got solid alternatives for CMD, PowerShell, and Python. Let's walk through each one:
PowerShell has built-in support for accessing file version info, including copyright, without any extra tools. Here's how to use it:
For a single executable:
# Replace the file path with your target executable (Get-Item "C:\Windows\System32\cmd.exe").VersionInfo.CopyrightThis will directly output the copyright string from the file's details.
For multiple files (e.g., all .exe files in a folder):
Get-ChildItem "C:\Windows\System32\*.exe" | ForEach-Object { # Create a custom object to display filename and copyright neatly [PSCustomObject]@{ FileName = $_.Name Copyright = $_.VersionInfo.Copyright } }This loops through each executable and prints a clean table of results.
CMD doesn't have a native command to read this specific property, but we can use a quick VBScript helper to get the job done. Here's the workflow:
Create a new text file named
GetCopyright.vbsand paste this code:Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile(WScript.Arguments(0)) Set objShell = CreateObject("Shell.Application") Set objFolder = objShell.Namespace(objFSO.GetParentFolderName(objFile.Path)) Set objFileInfo = objFolder.ParseName(objFSO.GetFileName(objFile.Path)) ' The number 13 corresponds to the "Copyright" property in the Details tab WScript.Echo "Copyright: " & objFolder.GetDetailsOf(objFileInfo, 13)Open CMD and run the script with your target file path:
cscript //nologo GetCopyright.vbs "C:\Windows\System32\cmd.exe"The
//nologoflag hides the script header, so you only get the copyright output.
If you prefer Python, you can use the pywin32 library to access the same file properties. Here's how:
First, install the required library:
pip install pywin32Use this script to get the copyright info:
import win32api import win32con def get_file_copyright(file_path): try: # Retrieve the file's version info block version_info = win32api.GetFileVersionInfo(file_path, "\\") # Get the language and code page details from the version info lang, codepage = win32api.GetFileVersionInfo(file_path, "\\VarFileInfo\\Translation")[0] # Build the path to the copyright string in the version info copyright_key = f"\\StringFileInfo\\{lang:04x}{codepage:04x}\\LegalCopyright" return win32api.GetFileVersionInfo(file_path, copyright_key) except Exception as e: return f"Failed to retrieve copyright: {str(e)}" # Example usage target_file = "C:\\Windows\\System32\\cmd.exe" print(f"Copyright for {target_file}:") print(get_file_copyright(target_file))This script handles language/codepage variations, so it works for files with different localized version info.
内容的提问来源于stack exchange,提问作者khan




